2 回答

TA貢獻1831條經驗 獲得超4個贊
你可以通過使用以下方面來做一個邊際的改進:ngSwitch
<ng-container *ngFor="let col of columne">
<td [ngSwitch]="col.field">
<ng-container *ngSwitchCase="'date'"> {{rowData[col.field] | date: 'dd/MMM/yyyy'}}</ng-container>
<ng-container *ngSwitchCase="'currency'"> {{rowData[col.field] | currency:'CAD':'code'}}</ng-container>
<ng-container *ngSwitchCase="'name'"> {{rowData[col.field] | uppercase}}</ng-container>
<ng-container *ngSwitchDefault>{{rowData[col.field]}}</ng-container
</td>
</ng-container>
但是我建議使用一些組件庫,如果您需要具有這樣的動態列的表,則可以提供一個表。由于您的其他選項基本上涉及滾動您自己的可重用表組件,這可能有些大材小用。

TA貢獻1828條經驗 獲得超3個贊
我和@bryan60持有同樣的看法。材料表可以讓您免于這樣的噩夢
const dataSource = [
{id: 1, name:"Test 1", currency: "Euro", status: "Active", date: "4th of April 2008" },
{id: 2, name:"Test 2", currency: "Dollar", status: "Active", date: "12nd of May 2005" }
];
displayedColumns: string[] = ['id', 'name', 'currency', 'status', 'date'];
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> Id </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="currency">
<th mat-header-cell *matHeaderCellDef> Currency </th>
<td mat-cell *matCellDef="let element"> {{1 | currency: element.currency}} </td>
</ng-container>
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef> Status </th>
<td mat-cell *matCellDef="let element"> {{element.status}} </td>
</ng-container>
<ng-container matColumnDef="date">
<th mat-header-cell *matHeaderCellDef> Date</th>
<td mat-cell *matCellDef="let element"> {{element.date | date: 'dd/MM/yyyy' }} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
添加回答
舉報