页脚单元格未显示在 Mat-Table 中的 *ngFor 中
Footer Cell not getting displayed inside *ngFor in Mat-Table
我想在 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> 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-footer-row-example.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>
我想在 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> 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-footer-row-example.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>