28

I am using ag-grid to bind values from a list, Is it possible to copy the value/data in the selected cell. I have tried to copy the value using ctrl+c but its not working is there any other way?

Please help me!

durai
  • 359
  • 1
  • 4
  • 12

12 Answers12

43

There is a flag which will allow you to select text and then CTRL+C will work.

enableCellTextSelection=true

This is not an enterprise config and can be at any time to enable cell text selection.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Vladi
  • 583
  • 5
  • 13
  • This allows the cells to be copied, but not the headers – ChrisMcQueen Nov 28 '19 at 10:51
  • This should be the answer – karthikdivi May 24 '20 at 12:00
  • I get this error: compiler.js:2175 Uncaught Error: Template parse errors: Can't bind to 'enableCellTextSelection' since it isn't a known property of 'ag-grid-angular'. 1. If 'ag-grid-angular' is an Angular component and it has 'enableCellTextSelection' input, then verify that it is part of this module. 2. If 'ag-grid-angular' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. 3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. – Naik Ashwini Aug 06 '21 at 10:25
  • It seems that in the latest versions of AG grid this is an enterprise feature as well. – aderchox Feb 21 '22 at 22:19
28

You can do this using CSS below:

.ag-font-style {
  user-select: initial;
  -moz-user-select: text;
  -ms-user-select: text;
  -webkit-user-select: text;
}

This should work in any of the browsers viz IE, Chrome, Mozilla and may be Safari.

Joshua
  • 5,032
  • 2
  • 29
  • 45
Vinod Tanguturi
  • 281
  • 3
  • 5
5

Many users are asking for this feature:

https://github.com/ceolter/ag-grid/issues/87

Anyway copy-paste features seems active only in enterprise version:

https://www.ag-grid.com/angular-grid-context-menu/index.php

RegRog
  • 373
  • 2
  • 12
2

if you using angular, add this module in your module ModuleRegistry:

import { ClipboardModule } from "@ag-grid-enterprise/clipboard";
public modules: ModuleRegistry[] = [
   ClipboardModule
];
belivine
  • 31
  • 3
1

In the column definitions, set editable = true, for example:

const columns = [{
    headerName: "Name",
    field: 'name',
    width: 150,
    editable: true
}];

Double-clicking on a cell brings up an editor with the current text pre-selected, which can be copied out with Ctrl + C.

See: Cell Editing Documentation

AlexO
  • 1,311
  • 12
  • 18
  • 3
    The downside of this being that now your users can edit cell content. My guess is most folks will want the ability to highlight and Ctrl+C the text without having to enable editing. – Matt Jun 07 '19 at 18:28
  • Enable "EDIT" cell content may cause side effects, this is not strongly recommended. – LHCHIN Aug 15 '19 at 07:23
1

Yes. They have API methods to access the grids built-in clipboard. I have recently implemented this into my own grid.

In the onCellFocussed event I am saving the most recent focus column to a member variable:

onCellFocused(event: CellFocusedEvent) {
    this.lastFocusColumn = [event.column]

This needs to be wrapped in an array for the correct type. Then in your onCopy() handler (the function that gets called when CTRL+C is press or copy is selected from the context menu):

onCopy(){
   this.gridOptions.api.copySelectedRowsToClipboard( false, this.lastFocusColumn )

The first argument is whether or not you want headers to be copied, which in my case and I think yours, should be set to false.

Sam Letts
  • 47
  • 4
  • 1
    i tried to copy entire ag grid table to clipboard ,but it is copied only headerof the table . my code is this.gridApi.copySelectedRowsToClipboard(this.props.data) // this.props.data have array ag grid data – John Ken Apr 05 '21 at 06:38
1

Hope this will work for

[enableCellTextSelection]="true"

<ag-grid-angular #agGrid [enableCellTextSelection]="true" [columnDefs]="columnDefs" [defaultColDef]="defaultColDef" [rowData]="xdrData"></ag-grid-angular>
1

This is dscribed in the official documentation.

If you want to use a regular text selection as if the grid were a regular table, set enableCellTextSelection=true and ensureDomOrder=true in the gridOptions.

https://www.ag-grid.com/javascript-data-grid/selection-overview/

0

I have solved this issue by creating generic directive which copies the text in specified css selector on to clipboard upon clicking ctrl+c.

this answer helped me at lot.

Here is my html:

<div copiable selector=".ag-cell-focus">
    <div  ag-grid="gridOptions" class="ag-bootstrap"></div>
</div>

Here is the directive code:

// keys: 17 - ctrl, 67 - c
angular.module('common').directive('copiable', function () {
    return function (scope, element, attrs) {
        var ctrlDown = false;
        element.bind("keydown", function (event) {
            if(event.which === 17) {
                ctrlDown = true;
            } else if (event.which == 67 && ctrlDown == true) {
              var text = angular.element(attrs.selector).text();
              console.log("selected text for ctrl+c", text);
              if (window.clipboardData && window.clipboardData.setData) {
                // IE specific code path to prevent textarea being shown while dialog is visible.
                return clipboardData.setData("Text", text); 

              } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
                var textarea = document.createElement("textarea");
                textarea.textContent = text;
                textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
                document.body.appendChild(textarea);
                textarea.select();
                try {
                    return document.execCommand("copy");  // Security exception may be thrown by some browsers.
                } catch (ex) {
                    console.warn("Copy to clipboard failed.", ex);
                    return false;
                } finally {
                    document.body.removeChild(textarea);
                   ctrlDown = false;
                }
              }     
            }
        });
    };
})

Note: I couldn't get keyup event to work so end up setting ctrlDown to false in finally.

Community
  • 1
  • 1
NiTiN
  • 1,022
  • 1
  • 16
  • 25
0

Unfortunately Vinod's CSS fix no longer works. This does:

.ag-theme-balham.ag-unselectable {
  -webkit-user-select: text !important;
  user-select: initial !important;
}
J_P
  • 599
  • 1
  • 9
  • 20
0

You have to use ::ng-deep along with .ag-unselectable for community edition

::ng-deep .ag-unselectable {
   -moz-user-select: text;
   -webkit-user-select: text;
    -ms-user-select: text;
    user-select: text;
}

for Enterprise edition you may check - Clipboard

Shyam
  • 392
  • 1
  • 3
  • 10
0

TS

gridOptions: GridOptions = {
    ...
    enableCellTextSelection: true,
};

HTML

<ag-grid-angular
...
[gridOptions]="gridOptions"
...></ag-grid-angular>
Achraf Farouky
  • 813
  • 10
  • 11