How to use paginator from material angular?

后端 未结 9 1850
清歌不尽
清歌不尽 2020-12-04 11:14

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,

9条回答
  •  难免孤独
    2020-12-04 11:26

    The tricky part here is to handle is the page event. We can follow angular material documentation up to defining page event function.

    Visit https://material.angular.io/components/paginator/examples for the reference.

    I will add my work with hours of hard work in this regard. in the relevant HTML file should look like as follows,

    
    
    

    Then go to the relevant typescript file and following code,

    declarations,

    @Input('data') customers: Observable;
    pageEvent: PageEvent;
    Page: number=0;
    Size: number=2;
    recordCount: number;
    pageSizeOptions: number[] = [2,3,4,5];
    

    The key part of page navigation as follows,

    pageNavigations(event? : PageEvent){
        console.log(event);
        this.Page = event.pageIndex;
        this.Size = event.pageSize;
        this.reloadData();
      }
    

    We require the following function to call data from the backend.

    reloadData() {
    
        this.customers = this.customerService.setPageSize(this.Page ,this.Size);
        this.customerService.getSize().subscribe(res => {
          this.recordCount = Number(res);
         
          console.log(this.recordCount);
        });
      }
    

    Before the aforementioned implementation, our services file should contain the following service call

    setPageSize(page: number, size: number): Observable {
        return this.http.get(`${this.baseUrl}?pageSize=${size}&pageNo=${page}`);
      }
    

    Then all set to go and enable pagination in our app. Feel free to ask related questions thank you.

提交回复
热议问题