在动态角度表上应用日期管道

问题描述

我的“数据”对象看起来像

[
 {"id": "abc001","lastinspectionDate": "2019-09-26T00:00:00.000Z","nextinspectionDate": "2021-09-26T00:00:00.000Z"},{"id": "abc002","lastinspectionDate":"2018-08-25T00:00:00.000Z","nextinspectionDate": "2020-08-25T00:00:00.000Z"}
]

我想在动态表中显示数据。下面是我的HTML代码

<table mat-table [dataSource]="dataSource" class="mt-2 mat-elevation-z0">
   <ng-container *ngFor="let column of allCols" [matColumnDef]="column">
      <th mat-header-cell *matHeaderCellDef>{{ column }}</th>
      <td mat-cell *matCellDef="let row">
         {{ column['lastinspectionDate'] ? (row[column]| date: 'mediumDate') : row[column] }}
      </td>
    </ng-container>
</table>

这是我的ts代码,用于定义表格的数据源。

this.dataSource.data = data;
this.pageSize = data.length;
let headers = Object.keys(data[0]);
this.allCols = headers;

我的日期条件管道不起作用。你们可以帮我还是给我另一种解决方案?我已经坚持了一个星期。非常感谢您的帮助。

解决方法

也许您应该尝试以下方法:

<table mat-table [dataSource]="dataSource" class="mt-2 mat-elevation-z0">
   <ng-container *ngFor="let column of allCols" [matColumnDef]="column">
      <th mat-header-cell *matHeaderCellDef>{{ column }}</th>
      <td mat-cell *matCellDef="let row">
         {{ column === 'lastInspectionDate' ? (row[column]| date: 'mediumDate') : row[column] }}
      </td>
    </ng-container>
</table>