我一直在尝试在不使用 3rd 方库的情况下以角度动态构建和分页我的表格。围绕这一愿望的一些考虑可能包括以下一些或更多:
- 每个表页的项目数。您希望用户指定它们还是硬编码?
- 处理点击事件,即第一个,最后一个
以下代码我用来动态生成页码(P2,...,Pn)
声明一个变量(pagenumbers)来保存页码HTML
pagenumbers = '<li class="page-item disabled clearfix d-none d-md-block"><a class="page-link waves-effect waves-effect">First</a></li><li class="page-item-disabled"><a class="page-link" aria-label="Previous"><span aria-hidden="true">«</span><span class="sr-only">Previous</span></a></li><li class="page-item active"><a class="page-link">1</a></li>';
调用服务获取客户列表:
async getCustomerOrders(CustomerID: number, customerService: CustomerService) {
await customerService.getCustomerOrders(CustomerID)
.map((res) => res)
.subscribe(
data => {
this.customerOrders = data;
this.tot = this.customerOrders.length;
console.log('Total records: ' + this.tot);
this.workingWithLoop(this.tot / 10);
},
err => console.log(err),
() => console.log(this.customerOrders.length / 10)
);
}
页码:
workingWithLoop(pages: number) {
// let pageNumbers = '';
console.log(this.pagenumbers);
console.log('Total pages: ' + pages);
for (let n = 2; n < pages; n++ ) {
console.log('Value of n: ' + n);
this.pagenumbers += '<li class="page-item"><a class="page-link waves-effect waves-effect">' + n + '</a></li>';
}
// tslint:disable-next-line:max-line-length
this.pagenumbers += ' <!--Arrow right--><li class="page-item"><a class="page-link" aria-label="Next"><span aria-hidden="true">»</span><span class="sr-only">Next</span></a></li><!--First--><li class="page-item clearfix d-none d-md-block"><a class="page-link">Last</a></li>';
}
在我的 html 中,使用表格底部的以下代码作为页码
<nav class="my-4 pt-2">
<ul class="pagination pagination-circle pg-purple mb-0" [innerHTML]="pagenumbers">
</ul>
</nav>
我仍在努力动态生成分页表。