0"-6ren">
gpt4 book ai didi

angular - 在 mat-table parent 中使用 ngif 时,mat-filtering/mat-sort 无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 17:12:15 29 4
gpt4 key购买 nike

请注意,分页/排序无法正常工作。分页不显示它显示的元素数量并且排序不起作用,但是如果您删除 html 文件中的行 *ngIf="dataSource?.filteredData.length > 0" 错误是固定的。如果使用过滤,它可以工作,但不显示过滤量

检查示例。

https://stackblitz.com/edit/angular-wqkekh-nm3pn2?file=app/table-pagination-example.html

最佳答案

在您的 component.ts 中,替换此代码

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;

ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}

用这个:

  private paginator: MatPaginator;
private sort: MatSort;


@ViewChild(MatSort) set matSort(ms: MatSort) {
this.sort = ms;
this.setDataSourceAttributes();
}

@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
this.paginator = mp;
this.setDataSourceAttributes();
}

setDataSourceAttributes() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}

在你的 html 中:

    <mat-card *ngIf="!dataSource?.filteredData.length" style="margin-bottom: 5px">
<div><span>ZERO RESULT</span></div>
</mat-card>

<mat-card *ngIf="dataSource?.filteredData.length">
** insert the table code that you have **
</mat-card>

关于angular - 在 mat-table parent 中使用 ngif 时,mat-filtering/mat-sort 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50767580/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com