我已经基于ng-bootstrap库(https://ng-bootstrap.github.io/#/components/table/examples)中Table组件的分页示例实现了带有分页的表。
在<ngb-pagination>
元素中,如下定义我的collectionSize
:
[collectionSize]="collectionSize || async"
这很好用,但是通过这种方式,我必须在API中专门为项目计数创建一个端点。如果我可以仅使用Content-Range
标头中的值,而该标头是在检索表中的项目时发送的,则容易得多。
我实现了表格和分页,如下所示:
HTML
<tbody *ngIf="items | async as rcvItems">
<tr *ngFor="let item of rcvItems">
<th scope="col">{{ item.x }}</th>
<td scope="col">{{ item.y }}</td>
</tr>
</tbody>
...
<ngb-pagination [collectionSize]="collectionSize | async" [(page)]="page" [pageSize]="pageSize" [maxSize]="5"
[rotate]="true" [ellipses]="false">
</ngb-pagination>
TS
ngOnInit(){
// count() gets the total amount of items
this.collectionSize = this.service.count();
// list() gets all the items
this.items = this.service.list(this.pageSize,(this.page - 1) * this.pageSize);
}
服务TS
list(limit,offset): Observable<Item[]> {
return this.http
.get(this.baseUrl + '?limit=' + limit + '&offset=' + offset)
.pipe(
map((data: any[]) =>
data.map(item => this.itemAdapter.adapt(item))
)
);
}
我试图通过添加list()
在tap(response => response.headers.get('Content-Range')),
方法中获取标头,但这似乎不起作用。
是否可以通过某种方式获取list()
方法接收到的项目的响应的标头,并将其用作collectionSize?
谢谢。