了解如何在Bootstrap-vue中使用分页 [英] Understanding how to use pagination in Bootstrap-vue
问题描述
我写了以下代码:
<div>
<div id="app" class="container">
<div class="grid">
<article v-for="tool in tools">
<div class="title">
<h3>{{capitalizeFirstLetter(tool.name)}}</h3>
</div>
<div class="description">
{{tool.description}}
</div>
<br>
<div>
<toggle-button :value=tool.status color="#82C7EB" :width=100 :height=30 :sync="true" :labels="{checked: 'Following', unchecked: 'Unfollowing'}" @change="onChange(tool)"/>
</div>
</article>
</div>
</div>
</div>
最近,我开始使用Bootstrap-Vue.我试图弄清楚如何在 pagination 上添加底部.我不确定 aria-controls
的工作方式.我的目标是在每个页面上有9个 tools
块.我应该如何添加分页,以便移至 tools
的下9个块?
Recently I started using Bootstrap-Vue. I'm trying to figure out how to add pagination on the bottom.
I'm not sure how aria-controls
works. My goal is to have 9 blocks of tools
on each page. How should I add the pagination so I could move to the next 9 blocks of tools
?
推荐答案
由于尚不清楚您是否需要客户端分页或服务器端,因此我在代码段中都做了一个示例.
Since it wasn't clear if you needed client-side pagination or serverside i made an example of both in the snippet.
为简单起见,我将其设为每页3个,但您可以将其更改为9个.
For simplicity I've made it 3 per page, but you could change it to 9.
第一个加载时会获取初始页面,然后每次使用 watcher
更改页面时都会调用API,该API会调用新页面的方法,该方法然后检索该位置并替换我们的旧数据与新数据.
The first one gets the initial page on load, and then calls the API every time the page changes by using a watcher
, that calls a method with the new page which then retrieves that place and replaces our old data with the new.
第二个在页面加载时加载所有数据,而是基于 per_page
属性对数据数组进行切片,以便仅显示该页面的项目.为此,我使用了一个计算属性,该属性会根据其中使用的属性自动更新.
The second one loads all the data on page load, and instead slices the data array based on the per_page
property, so that only the items for that page is shown.
For this I've used a computed property which automatically updates based on the properties used inside it.
这两个分页都具有 aria-controls
,它们是用我们页面元素容器的 id
定义的.用来告诉屏幕阅读器分页会更改哪些元素.
Both paginations have aria-controls
defined with the id
of the container of our page elements. This is used to tell screen readers what elements the pagination changes.
类 row
, col-*
, border
, mx-auto
, h2
和 text-center
是用于样式和布局的类,它不是实际解决方案的一部分,因此您可以自由地更改或删除它们.
The classes row
, col-*
, border
, mx-auto
, h2
and text-center
is classes used for styling and layout and isn't part of the actual solution, so you can freely change or remove them.
new Vue({
el: '#app',
computed: {
pagination2CurrentItems() {
const startIndex = (this.pagination2.current_page - 1) * this.pagination2.per_page;
const endIndex = startIndex + this.pagination2.per_page;
return this.pagination2.items.slice(startIndex, endIndex)
}
},
created() {
this.getPagination1Data()
this.getPagination2Data()
},
filters: {
capitalizeFirstLetter(value) {
return value.charAt(0).toUpperCase() + value.slice(1)
}
},
data() {
return {
pagination1: {
items: [],
per_page: 3,
total_rows: 0,
current_page: 1
},
pagination2: {
items: [],
per_page: 3,
total_rows: 0,
current_page: 1
}
}
},
methods: {
getPagination1Data(page = 1) {
fetch(`https://reqres.in/api/unknown?page=${page}&per_page=3`)
.then((response) => {
return response.json();
})
.then((data) => {
this.pagination1.total_rows = data.total;
this.pagination1.items = data.data;
});
},
getPagination2Data() {
/*
This endpoint only has 12 items total,
so this will get all in one call
*/
fetch(`https://reqres.in/api/unknown?per_page=12`)
.then((response) => {
return response.json();
})
.then((data) => {
this.pagination2.total_rows = data.total;
this.pagination2.items = data.data;
});
}
},
watch: {
'pagination1.current_page'(newPage) {
this.getPagination1Data(newPage)
}
}
})
<script src="https://unpkg.com/vue@2.6.11/dist/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.5.0/dist/bootstrap-vue.js"></script>
<link href="https://unpkg.com/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/bootstrap-vue@2.5.0/dist/bootstrap-vue.css" rel="stylesheet"/>
<div id="app" class="container">
<div id="tools_list_1" class="row">
<div class="col-12 h2 text-center">
Server pagination
</div>
<article class="col-3 mx-auto border" v-for="tool in pagination1.items">
<div class="title">
<h3>{{ tool.name | capitalizeFirstLetter }}</h3>
</div>
<div class="description">
{{ tool.pantone_value }}
</div>
</article>
</div>
<div class="row mt-2">
<div class="col-12">
<b-pagination
v-model="pagination1.current_page"
:per-page="pagination1.per_page"
:total-rows="pagination1.total_rows"
aria-controls="tools_list_1"
>
</b-pagination>
</div>
</div>
<div id="tools_list_2" class="row">
<div class="col-12 h2 text-center">
Client pagination
</div>
<article class="col-3 mx-auto border" v-for="tool in pagination2CurrentItems">
<div class="title">
<h3>{{ tool.name | capitalizeFirstLetter }}</h3>
</div>
<div class="description">
{{ tool.pantone_value }}
</div>
</article>
</div>
<div class="row mt-2">
<div class="col-12">
<b-pagination
v-model="pagination2.current_page"
:per-page="pagination2.per_page"
:total-rows="pagination2.total_rows"
aria-controls="tools_list_2"
>
</b-pagination>
</div>
</div>
</div>
这篇关于了解如何在Bootstrap-vue中使用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!