角材料-垫表动态页脚/页眉rowDef

时间:2020-08-29 08:24:37

标签: angular angular-material mat-table

我试图有条件地显示我的席子表的footer-row

基于this discussion,我知道以某种方式,我必须使用removeFooterRowDef删除它,而我猜想addFooterRowDefsetFooterRowDef要带另一个。

但是我找不到如何使用其中一种的正确解释。

请注意,我尝试将页脚行包装在ng容器中。

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

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

1 个答案:

答案 0 :(得分:0)

根据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,因为它会覆盖所有现有的页脚。

rowDefheaderRowDef的所有其他方法以相同的方式工作。我猜还有columnDef

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