Created
July 4, 2024 17:28
-
-
Save wilsaantos/2fcedb57f31506fd59a740ec383fe700 to your computer and use it in GitHub Desktop.
Angular Pagination In Frontend with Mat Paginator Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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