Search code examples
angularmean-stackprimeng

PrimeNG data tables - export selected rows


I have the following PrimeNG data table below and I'm trying to export to csv only the rows that I've selected where a selection column is defined here:

<p-column field="marked" header="Marked" [style]="{'width':'38px'}" selectionMode="multiple"></p-column>

Here is the entire data table defined:

<p-column field="marked" header="Selected Column" [style]="{'width':'38px'}" selectionMode="multiple"></p-column>
    <p-header>
        <div class="ui-helper-clearfix">
            <button type="button" pButton icon="fa-file-o" iconPos="left" label="Export To CSV" (click)="dt.exportCSV()" style="float:right"></button>
        </div>
    </p-header>
    <p-column field="marked" header="Selected Column" [style]="{'width':'38px'}" selectionMode="multiple"></p-column>
    <p-column field="id" header="count7" [sortable]="true"></p-column>
    <p-column field="name" header="count6" [sortable]="false"></p-column>
    <p-column field="field1" header="count5" [sortable]="true"></p-column>
    <p-column field="field2" header="count4" [sortable]="true"></p-column>
    <p-column field="field3" header="count3" [sortable]="true"></p-column>
    <p-column field="field4" header="count2" [sortable]="true"></p-column>
    <p-column field="field5" header="Count1" [sortable]="true"></p-column>
</p-dataTable>  

When I export this table now using the dt.exportCSV() command, the column "Selected Column" in the csv contains only the value 'undefined'.

So, I'd like to be able to only return either the selected rows, or have the rows in the csv be marked "true" or some other value. Thoughts?

Thanks for all the help!

EDIT: Adding my defined dt function:

   <p-dataTable #dt [value]="records" 
 [(selection)]="selectedRecords" exportFilename="discover" dataKey="id"
                [paginator]="true" [rows]="20" [headerCheckboxToggleAllPages]="true">

Solution

  • I know that question is old, but just in case someone is looking for an answer. Here is how we do it.

    dt.exportCSV({ selectionOnly: true });
    

    We are using PrimeNG version: 4.3.0.