1
votes

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">
2

2 Answers

2
votes

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.

0
votes

You haven't defined dt in your template. in primeNG samples, dt is defined as a template variable:

<p-dataTable #dt [value]="cars">
  <p-column field="vin" header="Vin"></p-column>
  <p-column field="year" header="Year"></p-column>
  <p-column field="brand" header="Brand"></p-column>
  <p-column field="color" header="Color"></p-column>
</p-dataTable> 

<button type="button" pButton icon="fa-file-o" iconPos="left" label="CSV" (click)="dt.exportCSV()"></button>

You need to define it as well like they do with the #dt syntax on your p-dataTable. Looks like it is intended to export the entire data table and not on individual columns though if that is what you're after.