Skip to content

Commit

Permalink
Merge pull request #14416 from Cr3aHal0/feature/csv-export-extra-columns
Browse files Browse the repository at this point in the history
fix: Do not take into account non exportable fields for csv separators
  • Loading branch information
cetincakiroglu authored Jan 4, 2024
2 parents ed8f7b6 + da53ec5 commit 34b177a
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/app/components/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2264,43 +2264,32 @@ export class Table implements OnInit, AfterViewInit, AfterContentInit, Blockable
}
}

//headers
for (let i = 0; i < (<any[]>columns).length; i++) {
let column = (<any[]>columns)[i];
if (column.exportable !== false && column.field) {
csv += '"' + this.getExportHeader(column) + '"';
const exportableColumns: any[] = (<any[]>columns).filter(column => column.exportable !== false && column.field);

if (i < (<any[]>columns).length - 1) {
csv += this.csvSeparator;
}
}
}
//headers
csv += exportableColumns.map(column => '"' + this.getExportHeader(column) + '"').join(this.csvSeparator);

//body
data.forEach((record: any, i: number) => {
csv += '\n';
for (let i = 0; i < (<any[]>columns).length; i++) {
let column = (<any[]>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 = '';

csv += '"' + cellData + '"';

if (i < (<any[]>columns).length - 1) {
csv += this.csvSeparator;
}
}
}
});
const body = data.map((record: any) =>
exportableColumns.map(column => {
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 = '';

return '"' + cellData + '"';
}).join(this.csvSeparator)
).join('\n');

if (body.length) {
csv += '\n' + body;
}

let blob = new Blob([csv], {
type: 'text/csv;charset=utf-8;'
Expand Down

0 comments on commit 34b177a

Please sign in to comment.