表格和分页器结合使用在大部分系统中使用都十分频繁。
为了为整个系统提供统一分页器的外观和初始设置,基于 element-ui 提供的 pagination 组件,来进行一个封装,是一个不错的选择。
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"
:current-page.sync="currentPage"
:page-size.sync="pageSize"
:layout="device === 'mobile'? layoutMobile : layout"
:page-sizes="pageSizes"
:total="total_1"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script>
import { scrollTo } from '@/utils/scroll-to'
import { mapGetters } from 'vuex'
function getPageList() {
let defaultPageList = [10, 50, 100, 200, 500]
const newPage = Number(localStorage.getItem('pageSize') || 10)
defaultPageList.push(newPage)
defaultPageList = [...new Set(defaultPageList.sort((a, b) => a - b))]
return defaultPageList
}
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number
},
page: {
type: Number,
default: 1
},
pagerCount: {
type: Number,
default: 10
},
limit: {
type: Number,
default: 10
},
pageSizes: {
type: Array,
default() {
return getPageList()
}
},
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
layoutMobile: {
type: String,
default: 'prev, total, next, jumper'
},
background: {
type: Boolean,
default: true
},
autoScroll: {
type: Boolean,
default: true
},
hidden: {
type: Boolean,
default: false
}
},
data() {
return {
tempCurrentPage: this.page,
tempPageSize: this.limit
}
},
computed: {
total_1() {
return this.total
},
currentPage: {
get() {
return this.tempCurrentPage
},
set(val) {
if (this.tempCurrentPage !== val) {
this.tempCurrentPage = val
this.$emit('update:page', val)
// console.log("pagenum change: ", val);
}
}
},
pageSize: {
get() {
return this.tempPageSize
},
set(val) {
if (this.tempPageSize !== val) {
this.tempPageSize = val
this.$emit('update:limit', val)
// console.log("pagesize change: ", val);
}
}
},
...mapGetters(['device'])
},
methods: {
resetPagination() {
this.pageSize = 10
this.currentPage = 1
},
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
if (this.autoScroll) {
scrollTo(0, 800)
}
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, limit: this.pageSize })
if (this.autoScroll) {
scrollTo(0, 800)
}
}
}
}
</script>
<style scoped>
.pagination-container {
background: transparent;
padding: 32px 16px;
}
.pagination-container.hidden {
display: none;
}
</style>
(1)引入自定义的分页器组件(组件路径视实际情况改动):
import Pagination from "@/components/Pagination";
(2)在组件实例中的 data 函数返回的对象数据中,定义分页器所需数据:
pagination: {
total: 0,
listQuery: {
page: 1,
limit: 10
}
}
(3)在 template 需要的地方,添加 Pagination 组件:
"ref"
:total="pagination.total"
:page.sync="pagination.listQuery.page"
:limit.sync="pagination.listQuery.limit"
@pagination="getList"
/>
(4)在 script 中添加分页交互逻辑:
getList(page, limit) {
// 当分页器页码或分页数发生改变时,重新请求表格数据
const pageConfig = {};
pageConfig.pageNum = Number(page.page);
pageConfig.pageSize = Number(page.limit);
// 基于新的分页数据,重新请求表格数据
this.getTableData(pageConfig);
}