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?
Asked
Active
Viewed 1.2k times
4
-
A property of the object is enough. – Quan VO Apr 03 '18 at 20:40
-
Whats this datakey actually means? – Aashiq Apr 05 '20 at 12:28
1 Answers
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' }
];
}
}

Jon
- 96
- 6