【发布时间】:2018-11-26 12:14:26
【问题描述】:
我正在尝试实现服务器端分页,但我不知道该怎么做,我在这里检查了多个答案,但我没有找到可以让它工作的东西。
我正在尝试使用 ngx-pagination:https://github.com/michaelbromley/ngx-pagination#readme
这是我大摇大摆的回应:
{
"count": 34,
"next": "http://0.0.0.0:8000/api/transaction/history?page=2",
"previous": null,
"results": [...]
}
我现在正在研究如何实现它,我知道我需要导航到下一页和上一页,但不知道如何去做。这就是我的组件现在的样子:
transactionsModel: TransactionModel[] = [];
page = 1;
total: number;
constructor(private billingService: BillingHistoryService, private router: Router) { }
ngOnInit() {
this.getAllTransactions();
}
getAllTransactions() {
this.billingService.getTransactions(this.page)
.subscribe((response: any) => {
this.transactionsModel = response.results;
this.total = response.results.length;
}, (error) => {
});
}
someFunction(e) {
// next/prev need to be handled here I guess
}
next() {
this.page++;
this.router.navigate(['/billing-history'], {queryParams: {page: this.page}});
}
prev() {
this.page--;
this.router.navigate(['/billing-history'], {queryParams: {page: this.page}});
}
在我的服务中,我只有:
private getTransactionsUrl = environment.apiUrl + '/transaction/history';
constructor(private http: HttpRequestService) { }
getTransactions() {
return this.http.get(this.getTransactionsUrl);
}
我需要将页面作为参数添加到我的服务吗?以及如何触发下一个值:
<pagination-controls (pageChange)="someFunction($event)" id="billing-items"></pagination-controls>
提前致谢。
【问题讨论】: