代码之家  ›  专栏  ›  技术社区  ›  Dante Orlando

PrimeNG TurboTable:dataKey需要是列吗?

  •  4
  • Dante Orlando  · 技术社区  · 8 年前

    p-table上的dataKey属性需要定义为列还是只需要是[值]数组中对象的属性?如果需要是一列,该列是否需要可见?

    1 回复  |  直到 8 年前
        1
  •  3
  •   Jon    8 年前

    否,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>
    

    类型脚本:

    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

    推荐文章