Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use paginator from material angular?

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?

like image 450
Drunken Daddy Avatar asked Jul 26 '17 05:07

Drunken Daddy


People also ask

What is Mat-Paginator in Angular?

MatPaginator. Component to provide navigation between paged information. Displays the size of the current page, user-selectable options to change that size, what items are being shown, and navigational button to go to the previous or next page.

How do you get the current page number in pagination Angular material?

You can get the current page index by using the page Output event. The $event from page returns three pieces of information: pageIndex. pageSize.

What is pageIndex mat-Paginator?

The MatPaginator is a directive that provides navigation between paged information. The navigator displays the size of the current page, options to change the page size, buttons to go previous and next page.


1 Answers

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:

 <md-paginator #paginator                 [length]="length"                 [pageIndex]="pageIndex"                 [pageSize]="pageSize"                 [pageSizeOptions]="[5, 10, 25, 100]"                 (page)="pageEvent = getServerData($event)"                 >  </md-paginator> 

And later, you have to add the pageEvent attribute in the foo.component.ts class and the others to handle paginator requirements:

pageEvent: PageEvent; datasource: null; pageIndex:number; pageSize:number; length:number; 

And add the method that will fetch the server data:

ngOnInit() {    getServerData(null) ... }  public getServerData(event?:PageEvent){   this.fooService.getdata(event).subscribe(     response =>{       if(response.error) {         // handle error       } else {         this.datasource = response.data;         this.pageIndex = response.pageIndex;         this.pageSize = response.pageSize;         this.length = response.length;       }     },     error =>{       // handle error     }   );   return event; } 

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 they will be automatically calculated upon view rendering.

I hope this can help you. Let me know if you had success. =]

like image 100
aelkz Avatar answered Sep 30 '22 08:09

aelkz