w=12=wt ww=11=wt - w=10=wt

Angular Material v6 Mat-Footer - "Cannot read property 'template' of undefined"

最近为此目的升级到 angular 6 之后,我正在尝试将页脚行实现到 mat-table 组件中,但是在添加了 mat-footer-cell 和 mat-footer-row 中的元素 table,我得到以下错误:

ERROR TypeError: Cannot read property 'template' of undefined at MatFooterRowDef.push../node_modules/@angular/cdk/esm5/table.es5.js.CdkFooterRowDef.extractCellTemplate (vendor.js:17400)

table 仍然出现在页面上,但没有数据,没有页脚行,并且在每个列标题的右侧有一个 T 符号。

HTML:

  <ng-container matColumnDef="total">

    <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 15%; flex: none">Total</mat-header-cell>

    <mat-cell *matCellDef="let item" style="width: 15%; flex: none">{{item.total | currency: 'GBP'}}</mat-cell>

    <td mat-footer-cell *matFooterCellDef>100</td>

  </ng-container>

  <mat-header-row *matHeaderRowDef="tableColumns"></mat-header-row>

  <mat-row *matRowDef="let row; columns: tableColumns" (click)="editItem(row)"></mat-row>

  <tr mat-footer-row *matFooterRowDef="tableColumns"></tr>

</mat-table>

已修复:这在 material 文档中没有明确说明,但 所有 列必须包含 <mat-footer-cell/> 元素,即使只有一列将包含内容。

您可以定义自己的 "tableFooterColumns" 变量。

tableFooterColumns: string[] = ['total'];

并在模板中使用它(根据需要更改 "colspan"):

 <tr mat-footer-row *matFooterRowDef="tableFooterColumns" colspan="2"></tr>

需要为每一列添加 mat-footer-cell

<div class="example-container mat-elevation-z8">
  <mat-table #table [dataSource]="dataSource">

    <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>

    <ng-container matColumnDef="categoryName">
      <mat-header-cell *matHeaderCellDef> First Name </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.firstName}} </mat-cell>
      
      //This is the part for which the code is breaking
      <mat-footer-cell *matFooterCellDef> Your footer column Name will goes here </mat-footer-cell>**
    </ng-container>
    <ng-container matColumnDef="categoryName">
      <mat-header-cell *matHeaderCellDef> Last Name </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.lastName}} </mat-cell>
      
      //This need to be included in all your columns
      <mat-footer-cell *matFooterCellDef> Your 2nd footer column Name will goes here </mat-footer-cell>**
    </ng-container>
    <mat-footer-row *matFooterRowDef="displayedColumns; sticky: true"></mat-footer-row>
  </mat-table>
</div>