我有一个带有codesandbox的简单vue项目bootstrap-vue。
此专栏有几行包含卡片和分页的内容:
<template>
<b-container>
<b-row
:current-page="currentPage"
:per-page="perPage">
<b-col cols="12" sm="4" class="my-1"
v-for="item in items"
:key="item.id">
<b-card
:bg-variant="item.variant"
text-variant="white"
header="item.title"
class="text-center"
>
<p class="card-text">
{{item.body}}
</p>
</b-card>
</b-col>
</b-row>
<b-row>
<b-col md="6" class="my-1">
<b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" class="my-0" />
</b-col>
</b-row>
</b-container>
</template>
因此,我正在尝试对列执行工作分页,类似于对bootstrap table进行分页。
页面应显示两列,卡片= perPage: 2
。
问题:如何为自定义列或行设置bootstrap-vue perPage
?
答案 0 :(得分:1)
如果您希望每页获得2张卡片,我建议您使用计算属性来工作solution,我已经创建了一张名为currentPageItems
computed: {
...
currentPageItems() {
let lengthAll =this.items.length;
this.nbPages = 0;
for (let i = 0; i < lengthAll; i = i + this.perPage) {
this.paginated_items[this.nbPages] = this.items.slice(
i,
i + this.perPage
);
this.nbPages++;
}
return this.paginated_items[this.currentPage-1];
}
...
}
并且我在data()
中添加了这些属性:
paginated_items: {},
currentPageIndex:0,
nbPages:0
在模板部分将items
更改为currentPageItems
...
<b-col cols="12" sm="4" class="my-1" v-for="item in currentPageItems" :key="item.id">
...
完整代码段:
<template>
<b-container>
<b-row>
<b-col cols="12" sm="4" class="my-1"
:key="index"
v-for="(item, index) in currentPageItems"
>
<b-card
:bg-variant="item.variant"
text-variant="white"
:header="item.title"
class="text-center"
>
<p class="card-text">
{{item.body}}
</p>
</b-card>
</b-col>
</b-row>
<b-row>
<b-col md="6" class="my-1">
<b-pagination :total-rows="totalRows" :per-page="perPage" v-model="currentPage" class="my-0" />
</b-col>
</b-row>
</b-container>
</template>
<script>
const items = [
{
title: "Primary",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
variant: "primary"
},
{
title: "Secondary",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
variant: "secondary"
},
{
title: "Success",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
variant: "success"
},
{
title: "Info",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
variant: "info"
},
{
title: "Warning",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
variant: "warning"
},
{
title: "Danger",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
variant: "danger"
}
];
export default {
name: "MyBootstrapGrid",
data() {
return {
items: items,
currentPage: 1,
perPage: 2,
totalRows: items.length,
paginated_items: {},
currentPageIndex:0,
nbPages:0
};
},
computed: {
pageCount() {
let l = this.totalRows,
s = this.perPage;
return Math.floor(l / s);
},
currentPageItems() {
let lengthAll =this.items.length;
this.nbPages = 0;
for (let i = 0; i < lengthAll; i = i + this.perPage) {
this.paginated_items[this.nbPages] = this.items.slice(
i,
i + this.perPage
);
this.nbPages++;
}
return this.paginated_items[this.currentPage-1];
}
},
methods: {}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
答案 1 :(得分:0)
如果我正确地理解了您的意思,那么剩下要做的就是对这些项目进行分页。 这是工作中的Sandbox 我添加了 paginatedItems 数据以及 paginate 和 onPageChange 方法。