页脚单元格未显示在垫子表中的*ngFor内



我试图在表的底部显示总和。

我遵循了与文档中提到的完全相同的步骤,但仍然没有工作。

下面是我的模板代码

<table mat-table [dataSource]="transactions" matSort class="table table-hover">
<ng-container *ngFor="let columnName of displayedColumns" [matColumnDef]="columnName">
<th mat-header-cell *matHeaderCellDef mat-sort-header class="text-danger">{{ columnName }}</th>
<td mat-cell *matCellDef="let row">{{ row[columnName] }}</td>
<td mat-footer-cell *matFooterCellDef> Total </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns" (click)="getRecord(row)"></tr>
<td mat-footer-cell *matFooterCellDef>{{ total }}</td>
</table>

组件内

total = this.transactions.map(t => t.cost).reduce((acc, value) => acc + value, 0);

下面是我的工作示例。请告诉我我在这里做错了什么

https://stackblitz.com/edit/angular-ehc6fn?file=src%2Fapp%2Ftable-页脚-右侧示例.ts

试试这个。

<table mat-table [dataSource]="transactions" class="mat-elevation-z8">
<!-- Item Column -->
<ng-container matColumnDef="item">
<th mat-header-cell *matHeaderCellDef> Item </th>
<td mat-cell *matCellDef="let transaction"> {{transaction.item}} </td>
<td mat-footer-cell *matFooterCellDef> Total </td>
</ng-container>
<!-- Cost Column -->
<ng-container matColumnDef="cost">
<th mat-header-cell *matHeaderCellDef> Cost </th>
<td mat-cell *matCellDef="let transaction"> {{transaction.cost | currency}} </td>
<td mat-footer-cell *matFooterCellDef> {{getTotalCost() | currency}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
<tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>
</table>

检查工作演示

更新

<table mat-table [dataSource]="transactions" matSort class="table table-hover">
<ng-container *ngFor="let columnName of displayedColumns" [matColumnDef]="columnName">
<th mat-header-cell *matHeaderCellDef mat-sort-header class="text-danger">{{ columnName }}</th>
<td mat-cell *matCellDef="let row">{{ row[columnName] }}</td>
<td mat-footer-cell *matFooterCellDef>
{{columnName === 'item' ? 'Total': getTotalCost()}}
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns" (click)="getRecord(row)"></tr>
<tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>
</table>

更新的演示

最新更新