Ant?Design?Vue?Pagination分頁(yè)組件的封裝與使用
Ant Design Vue Pagination分頁(yè)組件的封裝與使用
今天封裝一個(gè)常用的組件 Pagination 分頁(yè)

因?yàn)槭?Ant Design Vue 的組件,所以必須安裝Ant Design Vue才能使用哦~
使用組件(可參考Ant Design Vue 快速上手):
$ npm i --save ant-design-vue
第一步:首先創(chuàng)建一個(gè)組件文件Pagination.vue ,完整代碼:
<template>
<div class="mz-antd-pagination">
<a-pagination
v-model="current"
:page-size-options="pageSizeOptions"
:total="total"
show-size-changer
:page-size="pageSize"
@showSizeChange="onShowSizeChange"
>
<template slot="buildOptionText" slot-scope="props">
<span>{{ props.value }}條/頁(yè)</span>
</template>
</a-pagination>
</div>
</template>
<script>
export default {
props: {
total:{
type: Number,
default: 0
},
pageSizeOptions: {
type: Array,
default() {
return ['10', '20', '30', '40', '50'];
}
},
},
data() {
return {
pageSize: 10,
current: 1
};
},
mounted() {
},
methods: {
onShowSizeChange(current, pageSize) {
this.pageSize = pageSize;
this.current = current;
this.$emit('onShowSizeChange', current, pageSize);
}
},
watch: {
current(val) {
this.$emit('onShowSizeChange', val, this.pageSize);
},
},
};
</script>
<style lang="scss" scoped>
.mz-antd-pagination{
.ant-pagination {
text-align: right !important;
margin-top: 20px !important;
}
}
</style>
第二步:使用方法
<template>
<div class="merchandise-news">
<Breadcrumb :routes='routes'></Breadcrumb>
<div class="goods-info">
<div class="table-list">
<!-- 表格 -->
<a-table
:columns="columns"
:data-source="tableData"
:locale='{emptyText:"暫無(wú)數(shù)據(jù)"}'
:pagination="false"
:scroll="{ x: 1300 }">
<!-- <span slot="action" slot-scope="text, record">
<a-button type="link" @click="goEditPage(record)">編輯</a-button>
</span> -->
</a-table>
<!-- 3.頁(yè)面使用分頁(yè)組件 -->
<Pagination
v-model="pagination.current"
:total="pagination.totalCount"
show-size-changer
:page-size="pagination.pageSize"
@onShowSizeChange="onShowSizeChange"
></Pagination>
</div>
</div>
</div>
</template>
<script>
import Pagination from "@/components/MzAntD/Pagination"; //1. 引入 Pagination.vue 組件,注意路徑哦~
import { getBaseStoreList} from "@/api/storeSmart/baseInfo"
export default {
data() {
return {
//表格數(shù)據(jù):
columns: [
{
title:'序號(hào)',
customRender: (text, record, index) => `${index + 1}`
},
{
title: '品牌',
dataIndex: 'brand',
key: 'brand',
ellipsis: true,
}
{
title: '狀態(tài)',
dataIndex: 'statsName',
key: 'statsName',
ellipsis: true,
},
{
title: '操作',
key: 'action',
scopedSlots: { customRender: 'action' },
}
],
// pageSizeOptions: ['10', '20', '30', '40', '50'], //自定義分頁(yè)
pagination:{ //分頁(yè)數(shù)據(jù)
current : 1,
pageSize: 10,
totalCount:500
},
}
},
components:{
Pagination //2. 在components中定義Pagination
},
mounted(){
this.getStoreList()
},
methods:{
// 獲取列表
async getStoreList(){
let param = {
pageNo:this.pagination.current,
pageSize: this.pagination.pageSize
};
let res = await getBaseStoreList(param);
let list = res.data.result.list //列表數(shù)據(jù)
this.tableData = list
this.pagination.totalCount = res.data.result.totalCount //表格長(zhǎng)度
},
// 分頁(yè)改變時(shí)調(diào)用組件里的方法
onShowSizeChange(current, pageSize) {
console.log(current, pageSize);
this.pagination.current = current
this.pagination.pageSize = pageSize;
//改變完 重新渲染數(shù)據(jù)
this.getStoreList()
},
}
}
</script>
<style scoped lang="scss">
</style>
設(shè)置ant design vue中的 pagination的最大分頁(yè)數(shù)
Ant Design Vue 中的 pagination 組件有一個(gè) pageSize 屬性,用于設(shè)置每頁(yè)顯示的數(shù)據(jù)條數(shù)。
同時(shí),還有一個(gè) total 屬性,用于設(shè)置數(shù)據(jù)總條數(shù)。通過(guò)計(jì)算可以算出分頁(yè)數(shù),從而實(shí)現(xiàn)設(shè)置最大分頁(yè)數(shù)的目的。
代碼示例:
<a-pagination
? :total="total"
? :pageSize="pageSize"
? @change="handlePageChange"
/>
?
<script>
export default {
? data() {
? ? return {
? ? ? total: 100,
? ? ? pageSiz總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
公共組件父子依賴調(diào)用及子校驗(yàn)父條件問(wèn)題解決
這篇文章主要介紹了如何解決公共組件父子組件依賴調(diào)用和子組件校驗(yàn)父組件條件的問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
vue.js綁定事件監(jiān)聽(tīng)器示例【基于v-on事件綁定】
這篇文章主要介紹了vue.js綁定事件監(jiān)聽(tīng)器的方法,結(jié)合實(shí)例形式分析了vue.js基于v-on事件綁定響應(yīng)鼠標(biāo)點(diǎn)擊相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
uniapp?vue3中使用webview在微信小程序?qū)崿F(xiàn)雙向通訊功能
微信小程序的存在許多功能上的限制和約束,有些情況不得不去使用webview進(jìn)行開(kāi)發(fā)實(shí)現(xiàn)需求,這篇文章主要給大家介紹了關(guān)于uniapp?vue3中使用webview在微信小程序?qū)崿F(xiàn)雙向通訊功能的相關(guān)資料,需要的朋友可以參考下2024-07-07
Vue2.0 v-for filter列表過(guò)濾功能的實(shí)現(xiàn)
今天小編就為大家分享一篇Vue2.0 v-for filter列表過(guò)濾功能的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue-cli3 karma單元測(cè)試的實(shí)現(xiàn)
這篇文章主要介紹了vue-cli3 karma單元測(cè)試的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
vue.js中toast用法及使用toast彈框的實(shí)例代碼
這篇文章主要介紹了vue.js中toast用法及使用toast彈框的實(shí)例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒加載,需要的朋友可以參考下2018-08-08
vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案
這篇文章主要介紹了vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Vue的filters(本地)或filter(全局)過(guò)濾常用數(shù)據(jù)類型解析
這篇文章主要介紹了Vue的filters(本地)或filter(全局)過(guò)濾常用數(shù)據(jù)類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

