I am using an angular material Table and paginator. I want to send pageSize and pageIndex to the BE so I can do paging from BE, cause we have massive data and its not possible in FE. how can I subscribe on pageSize change and PageIndex changes and get those selected numbers by the user to send to the BE.
Asked
Active
Viewed 928 times
1
-
1This is the answer that you are looking for: https://stackoverflow.com/a/45685002/2976876 – smithnblack Apr 19 '20 at 13:54
-
Thanks , I found my answer also here – Maryam Ghafarinia Apr 20 '20 at 07:02
-
Please be kind and vote my answer up, if it helped you. Thanks! – smithnblack Apr 20 '20 at 09:59
1 Answers
1
You can subscribe to the pager as follows:
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
],
})
export class ListComponent implements AfterViewInit {
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
data: MatTableDataSource<any>;
constructor(private http: HttpClient) {}
ngAfterViewInit(): void {
this.data = new MatTableDataSource([]);
this.data.paginator = this.paginator;
this.paginator.pageIndex = 0;
this.paginator.page.pipe(
startWith({}),
switchMap(() => {
const page = this.paginator.pageIndex + 1;
const itemsPerPage = this.paginator.pageSize;
return this.http.get(`api_url?page=${page}&itemsPerPage=${itemsPerPage}`);
}),
map((apiResponseData) => { return apiResponseData;}),
).subscribe((data) => {
this.data= new MatTableDataSource(data);
this.data._updateChangeSubscription();
});
}
}

Juan Pablo Moreno Martín
- 600
- 6
- 16