具有单个或多个数据源的两个 mat-paginator 不起作用

UI_*_*Dev 1 html javascript pagination typescript angular

我有一个 mat-table,每页最多可以显示 5/10/15 个条目。起初,我在底部有一个垫子分页器,效果很好。现在我被要求在表格顶部设置一个分页器,在表格底部设置另一个分页器,因此如果用户正在寻找位于顶端。

我试图将两个分页器链接到同一个数据源。但它没有奏效。因此,尝试创建 2 个数据源,但它也有一个限制。

唯一的限制是底部的分页器不能更改每页的项目,因为没有一种方法可以让我控制该属性。

此外,我无法直接设置页面索引或每页属性(表格未刷新),因此所有移动都是通过一些逻辑和分页器方法(如 previousPage() 或 lastPage())实现的。

谁能帮我这个?我对具有单个或多个数据源的解决方案感到满意。

工作代码-> STACKBLITZ

<mat-paginator #paginatorTop (page)="pageEvent = handlePageTop($event)" [length]="length" [pageSizeOptions]="[5, 10, 15]" showFirstLastButtons></mat-paginator>

<mat-paginator #paginatorBottom (page)="pageEvent = handlePageBottom($event)" [length]="length" [pageSizeOptions]="[5, 10, 15]" showFirstLastButtons></mat-paginator>
Run Code Online (Sandbox Code Playgroud)

app.component.ts

ngOnInit() {
    this.dataSource2.paginator = this.bottomPaginator;
    this.dataSource.paginator = this.topPaginator;

    this.topPaginator.length = this.dataSource.data.length;
    this.bottomPaginator.length = this.dataSource2.data.length;
  }


  public handlePageTop(e: any) {
    let {pageSize} = e;

    this.bottomPaginator.pageSize = pageSize;

    if(!this.topPaginator.hasNextPage()){
      this.bottomPaginator.lastPage();
    }else if(!this.topPaginator.hasPreviousPage()){
      this.bottomPaginator.firstPage();
    }else{
      if(this.topPaginator.pageIndex < this.bottomPaginator.pageIndex){
        this.bottomPaginator.previousPage();
      } else  if(this.topPaginator.pageIndex >this.bottomPaginator.pageIndex){
        this.bottomPaginator.nextPage();
      }
    }
  }

  public handlePageBottom(e: any) {

    if(!this.bottomPaginator.hasNextPage()){
      this.topPaginator.lastPage();
    }else if(!this.bottomPaginator.hasPreviousPage()){
      this.topPaginator.firstPage();
    }else{
      if(this.bottomPaginator.pageIndex < this.topPaginator.pageIndex){
        this.topPaginator.previousPage();
      } else  if(this.bottomPaginator.pageIndex > this.topPaginator.pageIndex){
        this.topPaginator.nextPage();
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

Gos*_*dra 6

无需使用两个分页器对您的代码进行了一些更改,请检查它。

这是我的 stackblitz 代码:- https://stackblitz.com/edit/multiple-paginators-svwwfj

希望这可以帮助!