im using MatPaginator with MatTableDataSource and when I assign data to the dataSource it change the lenght i assign to the MatPaginator. So, when I load nextpage, sort or filter, dataSorce change the lenght and can't continue paginating.
My code:
public dataSource = new MatTableDataSource<Permission>();
pageSize!: number;
pageIndex!: number;
length!: number;
@ViewChild(MatPaginator)
paginator!: MatPaginator;
ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
}
handlePageEvent(event: PageEvent) {
this.loading = true;
this.pageSize = event.pageSize;
this.pageIndex = event.pageIndex + 1;
this.loadDatabase();
}
datab: Permission[] = [];
loadDatabase() {
this.permissionService
.showAllPermissions(this.filter, this.pageSize, this.pageIndex, this.sortColumn, this.sortOrder)
.subscribe(
result => {
const d = result.data as Permission[];
d.forEach(r => {
if (!this.datab.some(x => x.id === r.id)) {
this.datab.push(r);
}
});
this.dataSource.data = this.datab as Permission[];
this.length = result.paginatorInfo.total;
this.loading = false;
});
}
}
HTML
<table mat-table [dataSource]="dataSource" class="center" matSort (matSortChange)="sortData($event)">
<mat-paginator (page)="handlePageEvent($event)" [pageSize]=pageSize [length]="length" [pageIndex]="pageIndex-1">
</mat-paginator>
On Load:
After Next Page: