如何使用动态列数对 PrimeNG table 中的某些列使用日期管道

How to use Date Pipes for some columns in PrimeNG table with dynamic number of columns

我正在尝试显示动态数量的列,并且还尝试使用 <ng-template ... 来指定单元格的显示方式 rendered.But 在我的数据集中有一些字段属于日期类型。我想使用日期管道格式化该字段。

html 文件

<ng-template pTemplate="body" let-rowData let-columns="columns">
      <tr>
        <td *ngFor="let col of columns" [style.width]="col.width">
          {{rowData[col.field]| col?.pipe}}
        </td>
      </tr>
</ng-template>

typescript 文件(在 cols 数组中,我想像这样添加另一个字段。)

 this.cols = [
      { field: 'no', header: 'No.', width: '50px',pipe:null },
      { field: 'createdDate', header: 'createdDate', width: '175px', pipe: 'date: \'dd/MM/yyyy\''},
      { field: 'deviceName', header: 'Device Name', width: '150px'}
    ];

我已经试过了,它给我模板解析错误。

ERROR Error: Uncaught (in promise): Error: Template parse errors:

这样试试

this.cols = [
          { field: 'no', header: 'No.', width: '50px',pipe:null },
          { field: 'createdDate', header: 'createdDate', width: '175px', data: true , format: `dd/MM/yyyy`},
          { field: 'deviceName', header: 'Device Name', width: '150px'}
        ];

模板

<ng-template pTemplate="body" let-rowData let-columns="columns">
      <tr>
        <td *ngFor="let col of columns" [style.width]="col.width">   
          {{ col.data ? (rowData[col.field]| date : col.format) : rowData[col.field] }}
        </td>
      </tr>
</ng-template>

demo