4

Does the dataKey property on p-table need to be defined as a column or does it simply need to be a property of the objects in the [value] array? If it needs to be a column, does this column need to be visible?

Dante Orlando
  • 55
  • 1
  • 1
  • 6

1 Answers1

3

No, the dataKey does not need to be a column.

The dataKey should be a property of the record, but it doesn't need to be displayed in order to be used by the table.

HTML:

<p-table [columns]="cols" [value]="cars" [(selection)]="selectedCars" dataKey="vin">

    <ng-template pTemplate="header" let-columns>
        <th *ngFor="let col of columns">
            {{col.header}}
        </th>
    </ng-template>

    <ng-template pTemplate="body" let-car>
        <tr>
            <td>{{car.year}}</td>
        </tr>
    </ng-template>

</p-table>

Typescript:

export class TableDemo implements OnInit {

    cars: Car[];

    cols: any[];

    constructor() { }

    ngOnInit() {
        this.cars = [
            { vin: '123ABC', year: 1994 },
            { vin: '234BCD', year: 1978 },
            { vin: '345CDE', year: 2015 },
        ];

        this.cols = [
            { field: 'year', header: 'Year' }
        ];
    }
}

PrimeNG Table Documentation

Jon
  • 96
  • 6