Ant?Design?Vue?Pagination分頁組件的封裝與使用
Ant Design Vue Pagination分頁組件的封裝與使用
今天封裝一個常用的組件 Pagination 分頁
因為是 Ant Design Vue 的組件,所以必須安裝Ant Design Vue才能使用哦~
使用組件(可參考Ant Design Vue 快速上手):
$ npm i --save ant-design-vue
第一步:首先創(chuàng)建一個組件文件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 }}條/頁</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:"暫無數(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.頁面使用分頁組件 --> <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:'序號', 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'], //自定義分頁 pagination:{ //分頁數(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 //表格長度 }, // 分頁改變時調(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的最大分頁數(shù)
Ant Design Vue 中的 pagination 組件有一個 pageSize 屬性,用于設(shè)置每頁顯示的數(shù)據(jù)條數(shù)。
同時,還有一個 total 屬性,用于設(shè)置數(shù)據(jù)總條數(shù)。通過計算可以算出分頁數(shù),從而實現(xiàn)設(shè)置最大分頁數(shù)的目的。
代碼示例:
<a-pagination ? :total="total" ? :pageSize="pageSize" ? @change="handlePageChange" /> ? <script> export default { ? data() { ? ? return { ? ? ? total: 100, ? ? ? pageSiz
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Vue render函數(shù)在ElementUi中的應(yīng)用
今天小編就為大家分享一篇淺談Vue render函數(shù)在ElementUi中的應(yīng)用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue中動態(tài)添加style樣式的幾種寫法總結(jié)
這篇文章主要介紹了vue中動態(tài)添加style樣式的幾種寫法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue如何使用formData傳遞文件類型的數(shù)據(jù)
這篇文章主要介紹了vue如何使用formData傳遞文件類型的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05