Currently, I am faced with the task of exporting data from a PrimeNG Turbo table to a CSV file. The challenge lies in the fact that the 'rows' variable in the code snippet below is an array of objects which are displayed in the table body using ngIf based on their keys. While displaying them poses no problem, attempting to export to CSV results in the object being shown in the cell rather than its properties. Is there any way to extract the object property using datatable.exportCSV()?
Here is a snippet of my work:
<p-table #dt [columns]="cols" [value]="rows" selectionMode="multiple" [(selection)]="selectedResults">
<ng-template pTemplate="caption">
Test Set - {{testsetid}}
<div class="ui-helper-clearfix">
<button type="button" pButton icon="fa fa-file-o" iconPos="left" label="All Data" (click)="dt.exportCSV()" style="float:left"></button>
<button type="button" pButton icon="fa fa-file" iconPos="left" label="Selection Only" (click)="dt.exportCSV({selectionOnly:true})" style="float:right"></button>
</div>
</ng-template>
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of cols">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-row let-columns="cols">
<tr [pSelectableRow] = "row">
<td *ngFor="let col of cols" [ngStyle]="{'background-color': getColor(row[col.field]['status'])}">
<span *ngIf="col.field == 'script_name'" >{{row[col.field]}}</span>
<span *ngIf="col.field != 'script_name'" >
<a [routerLink]="['/localrunreport' , row[col.field]['sessionId']]"> {{row[col.field]['status']}}</a>
</span>
</td>
</tr>
</ng-template>
</p-table>
In the exported CSV file, you can see the issue depicted here: exported csv
The root cause of this problem stems from the fact that the array of objects is structured as follows:
[{'script_name': value, 'device_name':{'status':value,'sessionId':value}}}
Hence, the obstacle arises because the key 'device_name' holds an object as its value. Is there a method through which I can include the object's property in the CSV file?