From ab589686f10ffc67b955293f0af0003f7d44b9de Mon Sep 17 00:00:00 2001 From: cagataycivici Date: Tue, 4 Aug 2020 15:46:23 +0300 Subject: [PATCH] Fixed #413 - DataTable: Exporting an empty DataTable throws TypeError --- src/components/datatable/DataTable.vue | 52 +++++++++++++------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/components/datatable/DataTable.vue b/src/components/datatable/DataTable.vue index 2e607c62e..4d1bb8f81 100755 --- a/src/components/datatable/DataTable.vue +++ b/src/components/datatable/DataTable.vue @@ -953,35 +953,37 @@ export default { } //body - data.forEach(record => { - csv += '\n'; - for (let i = 0; i < this.columns.length; i++) { - let column = this.columns[i]; - if (column.exportable !== false && column.field) { - let cellData = ObjectUtils.resolveFieldData(record, column.field); - - if (cellData != null) { - if (this.exportFunction) { - cellData = this.exportFunction({ - data: cellData, - field: column.field - }); + if (data) { + data.forEach(record => { + csv += '\n'; + for (let i = 0; i < this.columns.length; i++) { + let column = this.columns[i]; + if (column.exportable !== false && column.field) { + let cellData = ObjectUtils.resolveFieldData(record, column.field); + + if (cellData != null) { + if (this.exportFunction) { + cellData = this.exportFunction({ + data: cellData, + field: column.field + }); + } + else + cellData = String(cellData).replace(/"/g, '""'); } else - cellData = String(cellData).replace(/"/g, '""'); - } - else - cellData = ''; - - - csv += '"' + cellData + '"'; - - if (i < (this.columns.length - 1)) { - csv += this.csvSeparator; + cellData = ''; + + + csv += '"' + cellData + '"'; + + if (i < (this.columns.length - 1)) { + csv += this.csvSeparator; + } } } - } - }); + }); + } let blob = new Blob([csv], { type: 'text/csv;charset=utf-8;'