p-table 上的 dataKey 属性是否需要定义为列,还是只需要是 [value] 数组中对象的属性?如果它需要是一个列,这个列是否需要可见?
最佳答案
不,dataKey 不需要是一列。
dataKey 应该是记录的一个属性,但它不需要显示出来才能被表使用。
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
关于PrimeNG TurboTable : Does dataKey need to be a column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49560815/