正如@rickvdbosch 所说,TableContinuationToken
预计只会向前发展。在分页器中进行一些更改后,我只能向前和向后移动。看起来不错,对我有用:
如果有人感兴趣。以下是更改:
- 实现您自己的
MatPaginatorIntl
以删除页面标签。我的样子是这样的:
@Injectable()
export class LogsPaginator extends MatPaginatorIntl {
public getRangeLabel = function (page: number, pageSize: number, length: number) {
return '';
};
}
- 缓存项目,您之前已加载,因为我们只能使用TableContinuationToken向前移动。您的 component.ts 应如下所示:
export class LogsComponent {
// As table storage does not support paging per index, we should cache already loaded logs and use continuation token if needed.
private cachedLogs: ILog[] = [];
private cachedIndexes: number[] = [];
private continuationToken = '';
ngOnInit() {
this.paginator.page.subscribe(this.pageChanged.bind(this));
}
async ngAfterViewInit() {
await this.loadLogs();
}
private async pageChanged(event: PageEvent) {
if (event.previousPageIndex < event.pageIndex && this.cachedIndexes.indexOf(event.pageIndex) === -1) {
await this.loadLogs();
} else {
this.redrawTable();
}
}
private redrawTable() {
const start = this.paginator.pageIndex * this.paginator.pageSize;
const end = start + this.paginator.pageSize;
this.logsTableSource.data = this.cachedLogs.slice(start, end);
}
private async loadLogs() {
const res = await this.myService.GetLogs(this.paginator.pageSize, this.continuationToken).toPromise();
this.cachedIndexes.push(this.paginator.pageIndex);
this.cachedLogs.push(...res.items);
this.paginator.length = res.isFinalPage ? this.cachedLogs.length : this.cachedLogs.length + 1;
this.continuationToken = res.continuationToken;
this.redrawTable();
}
}