How to use paginator from material angular?

后端 未结 9 1838
清歌不尽
清歌不尽 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:25

    Hey so I stumbled upon this and got it working, here is how:

    inside my component.html

    
    
    

    Inside my component.ts

    public array: any;
    public displayedColumns = ['', '', '', '', ''];
    public dataSource: any;    
    
    public pageSize = 10;
    public currentPage = 0;
    public totalSize = 0;
    
    @ViewChild(MatPaginator) paginator: MatPaginator;
    
    constructor(private app: AppService) { }
    
    ngOnInit() {
      this.getArray();
    }
    
    public handlePage(e: any) {
      this.currentPage = e.pageIndex;
      this.pageSize = e.pageSize;
      this.iterator();
    }
    
    private getArray() {
      this.app.getDeliveries()
        .subscribe((response) => {
          this.dataSource = new MatTableDataSource(response);
          this.dataSource.paginator = this.paginator;
          this.array = response;
          this.totalSize = this.array.length;
          this.iterator();
        });
    }
    
    private iterator() {
      const end = (this.currentPage + 1) * this.pageSize;
      const start = this.currentPage * this.pageSize;
      const part = this.array.slice(start, end);
      this.dataSource = part;
    }
    

    All the paging work is done from within the iterator method. This method works out the skip and take and assigns that to the dataSource for the Material table.

提交回复
热议问题