【发布时间】:2020-07-15 17:31:54
【问题描述】:
我有一个垫表,每页最多可显示 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>
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();
}
}
}
【问题讨论】:
-
也许你可以将你的代码上传到 StackBlitz ?!
-
是的,问题本身就在那里。 stackblitz.com/edit/multiple-paginators-knhf4q
-
问题不在于设计兄弟,它与每页大小的底部分页项目有关。数据源未更新
标签: javascript html angular typescript pagination