Pagination公共组件/components/Pagination/index.vue
<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>
import { scrollTo } from '@/utils/scroll-to'
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 [5, 10, 20, 30, 50]
}
},
layout: {
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', { current: this.currentPage, size: val })
if (this.autoScroll) {
scrollTo(0, 800)
}
},
handleCurrentChange(val) {
this.$emit('pagination', { current: val, size: this.pageSize })
if (this.autoScroll) {
scrollTo(0, 800)
}
}
}
}
</script>
<style scoped>
.pagination-container {
background: #fff;
padding: 32px 16px 16px 0;
margin-top: 0;
}
.pagination-container.hidden {
display: none;
}
</style>
页面使用
<template>
<div>
<!-- 分页组件 -->
<pagination
:limit.sync="queryParams.pageSize"
:page.sync="queryParams.pageNum"
:total="Number(tableData.total)"
@pagination="search"
v-show="tableData.total > 0"
/>
</div>
</template>
<script>
import Pagination from "@/components/Pagination";
export default {
components: { Pagination },
data() {
return {
loading: false,
queryParams: {
pageSize: 10,
pageNum: 1,
},
tableData: {
total: 0,
content: [],
},
date: "", //时间搜索v-module绑定的值
};
},
mounted() {
this.search();
},
methods: {
search(callback) {
let params = {
...this.queryParams,
};
//这个是带有日期选择的搜索
if (this.date) {
(params.beginDate = this.date[0]), (params.endDate = this.date[1]);
}
this.loading = true;
//请求接口
Api.getList(params, this).then((res) => {
this.loading = false;
if (typeof callback === "function") {
callback(res.data.totalElements);
} else {
this.tableData.records = res.data.content;
this.tableData.total = res.data.totalElements;
}
});
},
},
};
</script>
<style lang="scss" scoped>
</style>
此博客用于个人记录,组件适用于大部分项目,表格渲染、分页、搜索都具备。