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

前端 未结 5 1839
甜味超标
甜味超标 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条回答
  • 2021-02-08 13:09

    I have faced issue in hiding mat-paginator using *ngIf so I fixed it using below workaround. Define the below style in global.scss and apply the this style conditionally in mat-paginator

    .hide-paginator {
      display: none !important;
    }
    
    <mat-paginator 
    [length]="totalElements"
          [pageSize]="searchDefinition.size" [pageSizeOptions]="paginationOptions" 
          [ngClass]="{'hide-paginator': data.length <= 0 }">
    </mat-paginator>
      
    
    0 讨论(0)
  • 2021-02-08 13:13

    Just adding static:false instead true, working fine @ViewChild(MatSort, {static: false})e }) public sortaddEpisode: MatSort;

    0 讨论(0)
  • 2021-02-08 13:13

    Adding <tr> tag in your html code and adding mat-sort-header="fieldname" you can get resolved.

    0 讨论(0)
  • 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:

        <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>
    
    0 讨论(0)
  • 2021-02-08 13:26

    This can be solved by the following strategy:

    dataSource = new MatTableDataSource();
    
    @ViewChild(MatSort, {static: false}) set content(sort: MatSort) {
      this.dataSource.sort = sort;
    }
    

    Now even if you use *ngIf, it will work.

    0 讨论(0)
提交回复
热议问题