Multiple Pagination(Mat-table & Pagination): Multiple pagination in same component with different data source not working
See original GitHub issueDescription
I tried to put two tables in one component with different data sources which worked well with the data rending part. However, the paginator didn’t work on the second table.
Reproduction
Please use StackBlitz to check the issue: StackBlitz Project Editor link: https://stackblitz.com/edit/angular-brxwzo StackBlitz Output link: https://angular-brxwzo.stackblitz.io
Steps to reproduce:
- Create two table
- Create two data sources with different numbers of elements into it.
- Bind the data source with the respective data source for eg.
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol']; dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA); dataSource1 = new MatTableDataSource<PeriodicElement>(myData);
Table 1<table mat-table [dataSource]="dataSource">
Table 2<table mat-table [dataSource]="dataSource1">
- Create the paginator for the following datasouces:
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator; @ViewChild(MatPaginator, {static: true}) paginator2: MatPaginator;
- Only the first paginator will work in the output.
Expected Behavior
Like the first table pagination, the second table pagination should also work independently to the first as the data sources are different, the paginator pointing instance is different.
Actual Behavior
Only the first table pagination is working rest of all other table pagination are begin ignored.
Environment
- Angular: 9.0.5
- CDK/Material: 9.2.4
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): Ubuntu 20.01
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:11
Top Results From Across the Web
Multiple material pagination in one component doesn't work in ...
For mutiple MatPaginator and MatSort components present in single page you need yo use @ViewChildren(MatPaginator) paginator = new ...
Read more >Multiple mat-table in same component with matSort & mat ...
Let's go through an example to understand it further. We will take the employee table example <table mat-table [dataSource]="dataSource" class= ...
Read more >Multiple Paginators - StackBlitz
import { Component, OnInit, ViewChild,. ChangeDetectorRef } from '@angular/core';. import { MatPaginator, MatTableDataSource,.
Read more >Table | Angular Material
The mat-table provides a Material Design styled data-table that can be used ... In fact, the table can work with any custom pagination...
Read more >How to handle two tables in one component
... have two different tables in one component which have separated data sources ... and pagination for the second table doesn't work properly:...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I could use 2 paginators in the same component with the following fix:
Name the paginator in the html file.
<mat-paginator #allPaginator="matPaginator" [pageSizeOptions]="[10, 25, 50]" showFirstLastButtons></mat-paginator>
Declare the paginator in the component ts file.
@ViewChild('allPaginator',{read: MatPaginator}) allPaginator: MatPaginator;
Indicate paginator to be used by data source in the component ts file.
this.datasource.paginator = this.allPaginator
;Same issue on Angular 7.2, even after defining the paginator in html like this
Table 1
TS
@ViewChild('MatPaginator1') MatPaginator1: MatPaginator;
HTML<mat-paginator #MatPaginator1></mat-paginator>
Table 2
TS
@ViewChild('MatPaginator2') MatPaginator2: MatPaginator;
HTML<mat-paginator #MatPaginator2></mat-paginator>