Exporting Selected Grid Rows while Preserving Data Operations
Environment
| Product | Progress® Kendo UI for Angular Grid |
Description
How can I export selected Grid rows to Excel and PDF while maintaining the current filtering, sorting, and grouping? When rows are selected across multiple pages, I want to export all selected rows with the current data operations applied.
This Knowledge Base article also answers the following questions:
- How to export only selected Grid rows with filtering, sorting, and grouping applied?
- Why do exported selected rows not reflect the Grid's data operations?
- How to export all data with filtering, sorting, and grouping when no rows are selected?
Solution
To export selected rows across pages while preserving the applied data operations, use the kendoGridSelectBy directive to persist selection and the process method to apply the operations during export.
To export selected rows to Excel with data operations preserved:
-
Use the
kendoGridSelectBydirective to store the entiredataItemin theselectedKeyscollection and preserve selection across pages.TSpublic selectedKeys: Product[] = []; public selectBy = (args: RowArgs) => { return args.dataItem; }; -
Create a helper method that determines which data to export (selected rows or all data) and applies the current data operations using the
processmethod.TSpublic gridState: State = { skip: 0, take: 10, sort: [], group: [], filter: undefined }; private getProcessedExportData(): Product[] { const dataToExport = this.selectedKeys.length > 0 ? this.selectedKeys : products; const allDataState = { ...this.gridState, take: products.length }; return process(dataToExport, allDataState).data; }To export all pages when there are no selected rows, override the
takefield passed in theprocessmethod and set it to the total number of records in the Grid. -
Use the
fetchDatacallback to call the helper method and return the processed data.TSpublic getExcelData = (): ExcelExportData => { return { data: this.getProcessedExportData(), group: this.gridState.group, }; };
To export selected rows to PDF with data operations preserved:
-
Use the
kendoGridSelectBydirective to store the entiredataItemin theselectedKeyscollection and preserve selection across pages.TSpublic selectedKeys: Product[] = []; public selectBy = (args: RowArgs) => { return args.dataItem; }; -
Create a helper method that determines which data to export (selected rows or all data) and applies the current data operations using the
processmethod.TSpublic gridState: State = { skip: 0, take: 10, sort: [], group: [], filter: undefined }; private getProcessedExportData(): Product[] { const dataToExport = this.selectedKeys.length > 0 ? this.selectedKeys : products; const allDataState = { ...this.gridState, take: products.length }; return process(dataToExport, allDataState).data; }To export all pages when there are no selected rows, override the
takefield passed in theprocessmethod and set it to the total number of records in the Grid. -
Since the PDF export captures the content that is currently visible on the page, create a second Grid off-screen and bind its data to a getter, which calls the helper method to return the processed data.
HTML<div style="position: fixed; top: 0; left: -10000px; width: 800px;"> <kendo-grid [data]="pdfExportData" #pdfGrid> <kendo-grid-column field="ProductName" title="Product Name"></kendo-grid-column> ... </kendo-grid> </div> -
Use the
saveAsPDFmethod to export the hidden Grid to PDF.TS@ViewChild('pdfGrid') public pdfGrid!: GridComponent; public onPdfExport(): void { this.pdfGrid.saveAsPDF(); }
The following example demonstrates the full implementation for both Excel and PDF export.