实现样式
组件页面
<template>
<div :class="{'hidden':hidden}" class="pagination-container">
<el-pagination
:background="background"
:current-page.sync="currentPage"
:page-size.sync="pageSize"
:layout="layout"
:page-sizes="pageSizes"
:total="total"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script>
export default {
name: 'Pagination',
props: {
total: { // 数据总条数
required: true,
type: Number
},
page: { // 当前页
type: Number,
default: 1
},
limit: { // 默认每页显示条数
type: Number,
default: 20
},
pageSizes: { // 配置每页显示多少条
type: Array,
default() {
return [10, 20, 30, 50]
}
},
layout: { // 显示样式 total:总数, sizes:页数, prev:上一页, pager:数字, next:下一页, jumper:跳转
type: String,
default: 'total, sizes, prev, pager, next, jumper'
},
background: {
type: Boolean,
default: true
},
autoScroll: {
type: Boolean,
default: true
},
hidden: {
type: Boolean,
default: false
}
},
computed: {
currentPage: {
get() {
return this.page
},
set(val) {
this.$emit('update:page', val)
}
},
pageSize: {
get() {
return this.limit
},
set(val) {
this.$emit('update:limit', val)
}
}
},
methods: {
handleSizeChange(val) {
this.$emit('pagination', { page: this.currentPage, limit: val })
},
handleCurrentChange(val) {
this.$emit('pagination', { page: val, limit: this.pageSize })
}
}
}
</script>
<style scoped>
.pagination-container {
background: #fff;
padding: 32px 16px;
}
.pagination-container.hidden {
display: none;
}
</style>
组件调用
需要引用和注册组件
// 分页组件引用
import Pagination from '@/components/component/PaginationView'
// 注册组件
components: { Pagination },
<!-- 分页组件使用 -->
<pagination v-show="pagination.total>0" :total="pagination.total" :page.sync="searchForm.page" :limit.sync="searchForm.limit" @pagination="getList" />
变量定义
data() {
return {
// 查询框
searchForm: {
page: 1, // 默认起始页
limit: 10, // 默认显示条数
realNameLike: '' // 查询条件
},
// 分页配置
pagination: {
total: ''
}
}
}