mat-filtering/mat-sort not work correctly when use ngif in mat-table parent

前端 未结 5 1841
甜味超标
甜味超标 2021-02-08 13:02

Note that paging/sort not work correctly. Paging does not show the number of elements it is showing and sorting does not work, but if you delete the line in the html file

5条回答
  •  -上瘾入骨i
    2021-02-08 13:19

    In your component.ts, replace this code

    @ViewChild(MatPaginator) paginator: MatPaginator;
    @ViewChild(MatSort) sort: MatSort;
    
    ngAfterViewInit() {
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
    }
    

    with this :

      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;
      }
    

    And in your html:

        
            
    ZERO RESULT
    ** insert the table code that you have **

提交回复
热议问题