From e08fd2d1d0f7d33513d0cc6ff42585881b717397 Mon Sep 17 00:00:00 2001 From: mertsincan Date: Mon, 6 Dec 2021 10:18:55 +0300 Subject: [PATCH] Fixed #1845 - Add data param to exportCSV method on DataTable --- src/components/datatable/DataTable.d.ts | 5 ++-- src/components/datatable/DataTable.vue | 38 +++++++------------------ src/components/utils/DomHandler.js | 25 ++++++++++++++++ src/components/utils/Utils.d.ts | 1 + 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/components/datatable/DataTable.d.ts b/src/components/datatable/DataTable.d.ts index 93d48e2f7..10d981159 100755 --- a/src/components/datatable/DataTable.d.ts +++ b/src/components/datatable/DataTable.d.ts @@ -1032,9 +1032,10 @@ export declare type DataTableEmits = { declare class DataTable extends ClassComponent { /** * Exports the data to CSV format. - * @param {DataTableExportCSVOptions} options - Export options. + * @param {DataTableExportCSVOptions} [options] - Export options. + * @param {Object[]} [data] - Custom exportable data. This param can be used on lazy dataTable. */ - exportCSV: (options?: DataTableExportCSVOptions) => void; + exportCSV: (options?: DataTableExportCSVOptions, data?: any[]) => void; } declare module '@vue/runtime-core' { diff --git a/src/components/datatable/DataTable.vue b/src/components/datatable/DataTable.vue index 191b27c0c..b33bbb1bf 100755 --- a/src/components/datatable/DataTable.vue +++ b/src/components/datatable/DataTable.vue @@ -997,14 +997,17 @@ export default { this.$emit('update:selection', _selection); }, - exportCSV(options) { - let data = this.processedData; + exportCSV(options, data) { let csv = '\ufeff'; - if (options && options.selectionOnly) - data = this.selection || []; - else if (this.frozenValue) - data = data ? [...this.frozenValue, ...data] : this.frozenValue; + if (!data) { + data = this.processedData; + + if (options && options.selectionOnly) + data = this.selection || []; + else if (this.frozenValue) + data = data ? [...this.frozenValue, ...data] : this.frozenValue; + } //headers let headerInitiated = false; @@ -1055,28 +1058,7 @@ export default { }); } - let blob = new Blob([csv], { - type: 'text/csv;charset=utf-8;' - }); - - if (window.navigator.msSaveOrOpenBlob) { - navigator.msSaveOrOpenBlob(blob, this.exportFilename + '.csv'); - } - else { - let link = document.createElement("a"); - link.style.display = 'none'; - document.body.appendChild(link); - if (link.download !== undefined) { - link.setAttribute('href', URL.createObjectURL(blob)); - link.setAttribute('download', this.exportFilename + '.csv'); - link.click(); - } - else { - csv = 'data:text/csv;charset=utf-8,' + csv; - window.open(encodeURI(csv)); - } - document.body.removeChild(link); - } + DomHandler.exportCSV(csv, this.exportFilename); }, resetPage() { this.d_first = 0; diff --git a/src/components/utils/DomHandler.js b/src/components/utils/DomHandler.js index 775515494..153f6bd57 100755 --- a/src/components/utils/DomHandler.js +++ b/src/components/utils/DomHandler.js @@ -507,5 +507,30 @@ export default { isTouchDevice() { return (('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)); + }, + + exportCSV(csv, filename) { + let blob = new Blob([csv], { + type: 'application/csv;charset=utf-8;' + }); + + if (window.navigator.msSaveOrOpenBlob) { + navigator.msSaveOrOpenBlob(blob, filename + '.csv'); + } + else { + let link = document.createElement("a"); + if (link.download !== undefined) { + link.setAttribute('href', URL.createObjectURL(blob)); + link.setAttribute('download', filename + '.csv'); + link.style.display = 'none'; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + } + else { + csv = 'data:text/csv;charset=utf-8,' + csv; + window.open(encodeURI(csv)); + } + } } } diff --git a/src/components/utils/Utils.d.ts b/src/components/utils/Utils.d.ts index fff257f6c..b9daf9ed5 100644 --- a/src/components/utils/Utils.d.ts +++ b/src/components/utils/Utils.d.ts @@ -49,6 +49,7 @@ export declare class DomHandler { static isIOS(): boolean; static isAndroid(): boolean; static isTouchDevice(): boolean; + static exportCSV(csv: any, filename: string): void; } export declare class ObjectUtils {