【问题标题】:Angular material - mat-table dynamic footer/header rowDef角材料 - 垫表动态页脚/标题 rowDef
【发布时间】:2021-06-24 13:43:35
【问题描述】:

我正在尝试有条件地显示我的 mat-table 的 footer-row。

基于this discussion,我知道不知何故,我必须使用removeFooterRowDef 来删除它,我猜想addFooterRowDef 或setFooterRowDef 带来另一个。

但我找不到关于如何使用其中之一的正确解释。

请注意,我已尝试将页脚行包装在 ng-container 中。

<ng-container *ngIf="hasFooter">
    <tr mat-footer-row *matFooterRowDef="footerRowDef"></tr>
<ng-container>

但即使hasFooter 为假,该行仍然存在。

【问题讨论】:

    标签: angular angular-material mat-table


    【解决方案1】:

    根据this question上的答案,我发现它与模板中的viewChild结合使用,有

    <ng-container>
        <tr *matFooterRowDef="columns" mat-footer-row></tr>
    </ng-container>
    

    在组件类中

    @ViewChild(MatFooterRowDef, {static: true}) footerDef: MatFooterRowDef;
    @ViewChild(MatTable, {static: true}) table: MatTable;
    
    removeFooter() {
        this.table.removeFooterRowDef(this.footerDef);
    }
    
    addFooter() {
        this.table.setFooterRowDef(this.footerDef);
    }
    

    要添加新页脚,我们会调用 addFooterRowDef,但我会调用 setFooterRowDef 来覆盖任何现有的页脚。

    rowDef 和 headerRowDef 的所有其他方法都以相同的方式工作。我猜还有columnDef。

    如果有人需要,我把它留在这里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2021-09-03
      • 2020-04-12
      相关资源
      最近更新 更多