Copying Row Data from the Grid by Using the Angular Material Clipboard Service
Environment
| Product | Progress® Kendo UI® Data Grid for Angular |
Description
How can I copy row data from the Kendo UI for Angular Grid component by using the Angular Material Clipboard service?
Solution
The Grid provides a built-in Clipboard feature that allows copying and pasting of cell data. For custom clipboard operations, you can follow the approach below.
The following solution demonstrates how to copy row data from the Grid component by using the Angular Material Clipboard service. You can customize the implementation to suit your needs.
-
Install the
@angular/cdkpackage:shnpm install @angular/cdk -
Import the
ClipboardModulein the module where you want to use the Clipboard service:tsimport { ClipboardModule } from '@angular/cdk/clipboard'; @NgModule({ ... imports: [ ... ClipboardModule ], ... }) -
Inject the
Clipboardservice in the component where you want to use it:tsimport { Clipboard } from '@angular/cdk/clipboard'; export class AppComponent { constructor(private clipboard: Clipboard) { } } -
Use the
cellClickevent of the Grid to get the row data and save it in a variable:html<kendo-grid [data]="gridData" [selectable]="selectableSettings" (cellClick)="onCellClick($event)" > </kendo-grid>tsexport class AppComponent { public rowData: string = ''; public selectableSettings: SelectableSettings = { mode: 'single' }; public onCellClick(event: CellClickEvent): void { this.rowData = JSON.stringify(event.dataItem); } } -
Add a button to the toolbar template of the Grid and bind its
clickevent to the customonCopyDatamethod:html<kendo-grid> <ng-template kendoGridToolbarTemplate> <button kendoButton (click)="onCopyData()">Copy Row Data</button> </ng-template> </kendo-grid> -
Use the
copymethod of the Clipboard service to copy the row data to the clipboard:tspublic onCopyData(): void { this.clipboard.copy(this.rowData); }