I want to export specific columns in p-datatable. I have export all records before, but now I want to export specific columns. How do I do that?
Asked
Active
Viewed 1,600 times
2 Answers
1
From PrimeNG doc
In order to exclude a column from the csv, use
[exportable]="false"
onp-column
.

Antikhippe
- 6,316
- 2
- 28
- 43
0
For getting a specific column there is one solution. Html Page:
<p-dataTable #dg [value]="groupdetails" selectionMode="multiple" resizableColumns="true" reorderableColumns="true" expandableRows="true" [paginator]="true" [rows]="5" [pageLinks]="3" [rowsPerPageOptions]="[5,10,20]" [loading]="ploader3">
<p-header>
<div class="ui-helper-clearfix">
<button class="btn btn-info" type="button" (click)="dg.exportCSV({selectionOnly:true})" style="float:left"><i class="fa fa-download" aria-hidden="true"></i> SELECTED EXPORT</button>
<button class="btn btn-info" type="button" (click)="export(dg)" style="float:right"><i class="fa fa-download" aria-hidden="true"></i> Export</button>
</div>
</p-header>
<p-column field="groupName" [filter]="true" [sortable]="true" header="groupName"></p-column>
<p-column field="groupId" [filter]="true" [sortable]="true" header="groupId"></p-column>
<p-column field="parentGroupId" [filter]="true" [sortable]="true" header="parentGroupId"></p-column>
</p-dataTable>
In Ts page:
public export(e){
const hiddenColumns: any[] = [];
e.columns.forEach((c) => {
if (c.field === "hiddencolumnName" ) {
hiddenColumns.push({field: c.field, col: c});
c.field = '';
} if (c.field === "hiddencolumnName2" ) {
hiddenColumns.push({field: c.field, col: c});
c.field = '';
}
}
e.exportCSV();
// restore their fields so still works in the future
hiddenColumns.forEach((hc) => {
hc.col.field = hc.field;
});
}
Add columns to a hidden array which you don't want to export.

Nandy
- 341
- 3
- 5