Skip to content

Instantly share code, notes, and snippets.

@wilsaantos
Created July 4, 2024 17:28
Show Gist options
  • Save wilsaantos/2fcedb57f31506fd59a740ec383fe700 to your computer and use it in GitHub Desktop.
Save wilsaantos/2fcedb57f31506fd59a740ec383fe700 to your computer and use it in GitHub Desktop.
Angular Pagination In Frontend with Mat Paginator Example
/*
HTML
<mat-paginator
[length]="data?.length"
[pageSize]="pageSize"
[pageIndex]="pageIndex"
[pageSizeOptions]="[5, 10, 25, 100]"
(page)="changePage($event)"
>
</mat-paginator>
*/
//Table
data: IData[] = [];
dataSource: MatTableDataSource<IData> = new MatTableDataSource<IData>();
//Paginator
pageIndex: number = 0;
pageSize: number = 5;
getData() {
this.dataService.findAll().subscribe({
next: (response) => {
this.data = response;
this.changePage();
},
error: (error) => {
console.error(error);
},
});
}
changePage(event?: any) {
if (event) {
this.pageIndex = event.pageIndex;
this.pageSize = event.pageSize;
this.pageIndex = event.pageIndex;
}
this.dataSource.data = this.data.slice(
this.pageIndex * this.pageSize,
(this.pageIndex + 1) * this.pageSize
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment