Angular MatPaginator not working

前端 未结 30 1545
情歌与酒
情歌与酒 2020-12-04 23:05

I have 2 components. Both have mat-table and paginators and pagination is working for one component and not working for the other component though the code is similar. Below

相关标签:
30条回答
  • 2020-12-04 23:42

    I resolved a similar issue by surrounding the instantiation with a timeout. Try this :

    setTimeout(() => this.dataSource.paginator = this.paginator);
    
    0 讨论(0)
  • 2020-12-04 23:43

    What worked for me was to do implement what Lonely suggested, regarding ChangeDetectorRef, and also, set an object with static: false, in the @ViewChild, like this:

      @ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
    
    0 讨论(0)
  • 2020-12-04 23:43

    for Angular version below 7, use read argument for MatPaginator.

    @ViewChild(MatPaginator, {read: true}) paginator: MatPaginator;
    

    this worked for me.

    Note that this works for Dynamically loaded table data.

    0 讨论(0)
  • 2020-12-04 23:45

    1st Solution

    Move mat-paginator from inside *ngIf div to outside

    2nd Solution

    use static false while declaring MatPaginator or MatSort

    @ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
    @ViewChild(MatSort, {static: false}) sort: MatSort;
    
    0 讨论(0)
  • 2020-12-04 23:45

    The question when the paginator is available in the view and can be retrieved and attached to the data source is the main crux of this issue and a common pitfall. Workarounds suggested here include using setTimeout() and ngAfterViewInit, are simply that - workarounds in the vein of "let's see how much we need to wait to make sure that the @ViewChild has set our component field with the correct paginator value".

    The correct methodology is to attach the @ViewChild to a property setter and set the data source paginator as soon (and as often) as that setter is called with a valid paginator.

    It is also very useful to have a single data source and not replace it every load (as I've seen many people do) - just bind a data source to the mat-table and update it's data field.

     <mat-table [dataSource]="dataSource" matSort>
    
          <ng-container matColumnDef="col1">
            <mat-header-cell *matHeaderCellDef mat-sort-header> Column1 </mat-header-cell>
            <mat-cell *matCellDef="let row"> {{row.col1}} </mat-cell>
          </ng-container>
    
          <ng-container matColumnDef="col2">
            <mat-header-cell *matHeaderCellDef mat-sort-header> Column2 </mat-header-cell>
            <mat-cell *matCellDef="let row"> {{row.col2}} </mat-cell>
          </ng-container>
    
          <!-- Different columns goes here -->
    
          <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
          <mat-row *matRowDef="let row; columns: displayedColumns;">
          </mat-row>
        </mat-table>
    
        <mat-paginator #scheduledOrdersPaginator [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
      </div>
    
    dataSource: MatTableDataSource<any> = new MatTableDataSource();
    displayedColumns = ['col1', 'col2', ... ];
    
    @ViewChild('scheduledOrdersPaginator') set paginator(pager:MatPaginator) {
      if (pager) this.dataSource.paginator = pager;
    }
    
    @ViewChild(MatSort) set sort(sorter:MatSort) {
      if (sorter) this.dataSource.sort = sorter;
    }
    
    ngOnInit(): void {
        this.loadData().subscribe(somearray => { this.dataSource.data = somearray; });
    }
    

    This approach should also solve the problem (noted by one of the commenters here) of having the paginator rendered late when hidden behind an *ngIf template - the paginator will be sent to the data source even if it gets rendered very late.

    0 讨论(0)
  • 2020-12-04 23:45

    The solution that I find is to to set the paginator after the data is successfully loaded.

    this._empService.GetEmpList().subscribe(
      data => {
        this.empListDataSource = new MatTableDataSource<empToShow>(Object.values(data))
        this.empListDataSource.paginator = this.paginator
      }
    )
    
    0 讨论(0)
提交回复
热议问题