Angular MatPaginator not working

前端 未结 30 1547
情歌与酒
情歌与酒 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:45
      <mat-paginator
            #scheduledOrdersPaginator
              (page)="pageEvent($event)">
            </mat-paginator>
             pageEvent(event){
             //it will run everytime you change paginator
               this.dataSource.paginator = this.paginator;
              }
    
    0 讨论(0)
  • 2020-12-04 23:46

    To paginate the table's data, add a <mat-paginator> after the table.

    If you are using the MatTableDataSource for your table's data source, simply provide the MatPaginator to your data source. It will automatically listen for page changes made by the user and send the right paged data to the table.

    Otherwise if you are implementing the logic to paginate your data, you will want to listen to the paginator's (page) output and pass the right slice of data to your table.

    For more information on using and configuring the <mat-paginator>, check out the mat-paginator docs.

    The MatPaginator is one provided solution to paginating your table's data, but it is not the only option. In fact, the table can work with any custom pagination UI or strategy since the MatTable and its interface is not tied to any one specific implementation.

    @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
    ngOnInit() {
      this.dataSource.paginator = this.paginator;
    }
    

    See also https://material.angular.io/components/table/overview

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

    Do check out this Stackoverflow Answer.

    Setting the paginator in typescript after HTML components getting loaded (*ngIf) fixed the issue for me.

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

    To make it work I had to set the paginator after the data was fetched from source

    getVariables() {
        this.activeRoute.params.subscribe(params => {
            if (params['id'] && (params['type'] === Type.CodeList)) {
                this.dataService
                    .getItems(this.currentLanguage, params['id'])
                    .subscribe((items: any) => {
                        this.dataSource.data = this.items;
                        this.dataSource.paginator = this.paginator;
                    })
            }
        })
    }
    
    0 讨论(0)
  • 2020-12-04 23:48

    After trying all the above-provided solutions finally the simple solution worked for me. I am posting for reference if anyone stuck.

    I am using Angular 8. Just add { static: false } in child reference

      @ViewChild(MatSort, { static: false }) sort: MatSort;
      @ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
    

    Got Solution From Here

    I was using this :

    @ViewChild(MatSort, { read: true, static: false }) sort: MatSort;

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

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

    Only change

    dataSource: MatTableDataSource<any>;
    

    for

    dataSource = new MatTableDataSource();
    
    dataSource = new MatTableDataSource();
    displayedColumns = ['col1', 'col2', ... ];
    @ViewChild('scheduledOrdersPaginator') paginator: MatPaginator;
    @ViewChild(MatSort) sort: MatSort;
    ngOnInit(): void {
        // Load data
        this.dataSource = new MatTableDataSource(somearray);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
    }
    
    0 讨论(0)
提交回复
热议问题