I'm new to angular and trying to implement pagination in my app. I am trying to use this material component.
With the code below, I can get length
, pagesize
, and pageSizeOptions
in my .ts
file
<md-paginator [length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
</md-paginator>
export class PaginatorConfigurableExample {
length = 100;
pageSize = 10;
pageSizeOptions = [5, 10, 25, 100];
}
}
but I can't seem to trigger a function to change the data on the table above when the page is changed. Also, how do I use the nextPage
, previousPage
, hasNextPage
, and hasPreviousPage
methods?
Hey so I stumbled upon this and got it working, here is how:
inside my component.html
Inside my component.ts
All the paging work is done from within the
iterator
method. This method works out theskip
andtake
and assigns that to thedataSource
for the Material table.Component:
HTML
After spending few hours on this i think this is best way to apply pagination. And more importantly it works. This is my paginator code
<mat-paginator #paginatoR [length]="length" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions">
Inside my component
@ViewChild(MatPaginator) paginator: MatPaginator;
to view child and finally you have to bind paginator to table dataSource and this is how it is donengAfterViewInit() {this.dataSource.paginator = this.paginator;}
Easy right? if it works for you then mark this as answer.I'm struggling with the same here. But I can show you what I've got doing some research. Basically, you first start adding the page @Output event in the foo.template.ts:
And later, you have to add the pageEvent attribute in the foo.component.ts class and the others to handle paginator requirements:
And add the method that will fetch the server data:
So, basically every time you click the paginator, you'll activate getServerData(..) method that will call foo.service.ts getting all data required. In this case, you do not need to handle nextPage and nextXXX events because it will be automatically calculated upon view rendering.
Hope this can help you. Let me know if you had success. =]
The issue in the original question is that you are not capturing "page" event, to resolve this you need to add
(page)='yourEventHandler($event)
' as an attribute in the md-paginator tag.check a working example here
You can also check the API docs here
Another way to link Angular Paginator with the data table using Slice Pipe.Here data is fetched only once from server.
View:
Component