【发布时间】:2022-01-12 19:17:06
【问题描述】:
我需要使用分页动态填充引导 vue 表。 API 每次调用仅返回 10 个对象,并且对于每个后续页面,必须将最后一个对象的 id 作为查询参数传递以获取接下来的 10 条记录。
例如:如果对https://www.example.com/ 的第一次 GET 调用中的最后一个 id 是“10”,那么在单击“第 2 页”时,GET 调用应该是 https://www.example.com/?page_id=10,依此类推,直到api 不返回任何记录。
我曾尝试使用提供程序功能,但 api 不接受页码作为参数,所以这对我没有帮助。
这是我的 b 表和分页的外观:
<template>
<b-table
striped
hover
:items="reports"
:fields="fields"
:current-page="currentPage"
:per-page="perPage"
:busy.sync="isBusy"
></b-table>
<b-pagination
v-model="currentPage"
:total-rows="totalRows"
:per-page="perPage"
></b-pagination>
</template>
<script>
export default {
name: "tutorials-list",
data() {
return {
reports: [],
currentReport: null,
currentIndex: -1,
title: "",
fields: [
{ key: "id", label: "ID", sortable: true, sortDirection: "desc" },
{ key: "type", label: "Type", sortable: true, class: "text-center" },
{
key: "reported by",
label: "Reported By",
sortable: true,
class: "text-center",
},
{ key: "actions", label: "Actions" },
],
perPage: 5,
totalRows: 1,
currentPage: 1,
isBusy: false,
primary_user_id: 1,
page_id: null
};
},
methods: {
async retrieveReports() {
this.isBusy = true
return await ReportsDataService.getAll(this.primary_user_id, this.page_id ? this.page_id : '')
.then((response) => {
const result = response.data["votes"];
this.reports = result.map((report) => ({
id: report.id,
type: report.report_type,
"reported by": report.reported_by,
}));
this.isBusy = false
this.totalRows = this.reports.length
this.page_id = this.reports[this.reports.length-1].id
console.log();
return this.reports
})
.catch((e) => {
this.isBusy = false
console.log(e);
});
},
</script>
我是前端框架的完全新手,因此非常感谢这里的任何帮助,谢谢!
【问题讨论】:
-
您可以使用 Bootstrap 表签出 github repo,使用动态 rest api 调用的 Bootrap 分页 jebasuthan.github.io/vue_crud_bootstrap 希望对您有所帮助。
标签: vue.js user-interface vuejs2 frontend bootstrap-vue