繁体   English   中英

页脚单元格未显示在 Mat-Table 中的 *ngFor 内

[英]Footer Cell not getting displayed inside *ngFor in Mat-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>

更新的演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM