angular - 分页在 Angular 材料中无法正常工作
问题描述
在我的网格视图中,分页无法正常工作。我在下面附上我的观点。
我在下面解释我的代码。
组件文件:
<table mat-table [dataSource]="dataSource" matSort (matSortChange)="sortChange($event)">
<mat-paginator [pageSizeOptions]="[1,10, 25, 50, 100]" [pageSize]="pageSize" [pageIndex]="pageIndex" [length]="totalCount" (page)="pageChange($event)" showFirstLastButtons></mat-paginator>
</table>
.ts file:
@ViewChild(MatPaginator,{static: false})
paginator: MatPaginator;
@ViewChild(MatSort,{static: false})
sort: MatSort;
dataSource = new MatTableDataSource<Stores>([]);
pageSize = 1;
pageIndex = 1;
totalCount : number;
ngOnInit() {
this.dataSource.paginator = this.paginator;
this.getStores(1, 10);
}
getStores(page, limit) {
let sortBy = this.direction ? this.direction == 'asc' ? this.sortName : '-' + this.sortName : 'updatedAt';
let filters = {
"page" : page,
"limit" : limit,
"sortby" : sortBy,
"searchvalue" : this.searchValue || ''
}
this.api.getStores(filters).subscribe(x => {
console.log('xx',x);
this.data = x['data'];
this.storeData = this.data;
this.dataSource.data = this.storeData;
this.dataSource.sort = this.sort;
// this.paginator.pageIndex = this.pageIndex - 1;
this.allchecked = false;
});
}
pageChange(event) {
this.pageIndex = this.paginator.pageIndex + 1;
this.getStores(this.paginator.pageIndex + 1, this.paginator.pageSize);
}
在这里,我总共从数据库中获取了 2 条记录,并且每页我选择1
了,但是这里所有记录都显示并且分页没有到来。我的要求是,如果每页数小于总记录获取,那么分页应该按预期工作。
解决方案
推荐阅读
- javascript - 如何获取输入类型号的值并放入全局变量JS
- mysql - 窗口函数 ROW_NUMBER() 使用变量 RUNNING TOTAL 更改处理 ORDER BY
- css - 如何右对齐 LINQPad 查询结果中的一列?
- github - 我是否为我们的团队正在尝试做的事情正确设置存储库?
- mongodb - Discord.js MongoDB 添加新对象问题
- java - 在 TextView 中计算并显示结果 - Android Studio, Java
- react-native - 传递参数来路由 React Native
- ibm-watson - 如何关闭语音转文本服务的脏话过滤器
- javascript - 在 DISTINCT 查询和两个表相关查询中添加 COUNT
- javascript - 将 UK EndDate 与 UTC 当前时间进行比较?