I try to create a component which includes two dataTables each of it with another dataSource. My Tables aren't visible right after the component is loaded because of my *ngIf
so that i couldn't use ngAfterViewInit()
instead i'm using a solution a user pointed out on Github:
private paginator: MatPaginator;
private reportingPaginator: MatPaginator;
private sort: MatSort;
private reportingSort: MatSort;
@ViewChild(MatSort) set matSort(ms: MatSort) {
this.sort = ms;
this.reportingSort = ms;
this.setDataSourceAttributes();
}
@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
this.paginator = mp;
this.reportingPaginator = mp;
this.setDataSourceAttributes();
}
setDataSourceAttributes() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.reportingDataSource.paginator = this.reportingPaginator;
this.reportingDataSource.sort = this.reportingSort;
}
But i still can't get it to work. My Pagination doesn't work, when both paginators are included into the @ViewChild(MatPaginator)
. If i only include one of the paginators
@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
this.reportingPaginator = mp;
this.setDataSourceAttributes();
}
or
@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
this.paginator = mp;
this.setDataSourceAttributes();
}
the one i included works fine! So what do i need to do to get both paginators work?
For mutiple MatPaginator and MatSort components present in single page you need yo use
in your code which will return you list of MatSort and MatPaginator defined in the order in which it is present int your page. Below is the complete implemetation
You can retrieve them by #id also:
In your HTML:
In your component TS: