我试图有条件地显示我的席子表的footer-row
。
基于this discussion,我知道以某种方式,我必须使用removeFooterRowDef
删除它,而我猜想addFooterRowDef
或setFooterRowDef
要带另一个。
但是我找不到如何使用其中一种的正确解释。
请注意,我尝试将页脚行包装在ng容器中。
<ng-container *ngIf="hasFooter">
<tr mat-footer-row *matFooterRowDef="footerRowDef"></tr>
<ng-container>
但是即使hasFooter
为假,该行仍然存在。
答案 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
,因为它会覆盖所有现有的页脚。
rowDef
和headerRowDef
的所有其他方法以相同的方式工作。我猜还有columnDef
。
如果有人需要,我会把它留在这里。