2021-06-11 16:59:16 +08:00
|
|
|
|
<template>
|
2023-05-15 11:42:21 +08:00
|
|
|
|
<div class="pagination" >
|
2022-09-15 15:12:33 +08:00
|
|
|
|
<el-pagination
|
2021-06-11 16:59:16 +08:00
|
|
|
|
ref="page"
|
|
|
|
|
|
@size-change="size"
|
|
|
|
|
|
@prev-click="prev"
|
|
|
|
|
|
@next-click="next"
|
|
|
|
|
|
@current-change="current"
|
|
|
|
|
|
:current-page="pageObj.pageNo"
|
|
|
|
|
|
:page-sizes="pageSizes?pageSizes:[20, 50, 100]"
|
|
|
|
|
|
:page-size="Number(pageObj.pageSize)"
|
2022-03-21 16:16:43 +08:00
|
|
|
|
:layout="layout"
|
2021-06-11 16:59:16 +08:00
|
|
|
|
:total="pageObj.total"
|
2023-03-02 20:37:21 +08:00
|
|
|
|
v-bind="bind"
|
2022-09-15 15:12:33 +08:00
|
|
|
|
>
|
|
|
|
|
|
<el-select v-model="pageSize" :placeholder="pageSize+$t('pageSize')" size="mini"
|
|
|
|
|
|
:popper-append-to-body="appendToBody" class="pagination-size-select" @change="size"
|
|
|
|
|
|
:popper-class="popClass" @visible-change="popperVisible">
|
|
|
|
|
|
<el-option v-for="(item, index) in pageSizes" :key="index" :label="item.label" :value="item.value"></el-option>
|
|
|
|
|
|
</el-select>
|
2021-06-11 16:59:16 +08:00
|
|
|
|
|
2022-09-15 15:12:33 +08:00
|
|
|
|
</el-pagination>
|
2021-06-11 16:59:16 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
2022-12-08 16:09:46 +08:00
|
|
|
|
import { defaultPageSize, storageKey } from '@/utils/constants'
|
|
|
|
|
|
|
2022-09-09 19:35:58 +08:00
|
|
|
|
import { urlParamsHandler, overwriteUrl } from '@/utils/tools'
|
2022-09-13 16:54:41 +08:00
|
|
|
|
import { ref } from 'vue'
|
|
|
|
|
|
import { useRoute } from 'vue-router'
|
|
|
|
|
|
import { parseInt } from 'lodash'
|
2021-06-11 16:59:16 +08:00
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'pagination',
|
|
|
|
|
|
// props: ['pageObj', 'tableId', 'postPageSizes'],
|
|
|
|
|
|
props: {
|
|
|
|
|
|
pageObj: {},
|
|
|
|
|
|
tableId: {},
|
|
|
|
|
|
postPageSizes: {},
|
|
|
|
|
|
appendToBody: { default: true },
|
2022-03-21 16:16:43 +08:00
|
|
|
|
popClass: {},
|
|
|
|
|
|
layout: {
|
|
|
|
|
|
type: String,
|
|
|
|
|
|
default: 'total, prev, pager, next, slot'
|
2023-03-02 20:37:21 +08:00
|
|
|
|
},
|
|
|
|
|
|
bind: {
|
|
|
|
|
|
type: Object,
|
|
|
|
|
|
default: () => {}
|
|
|
|
|
|
},
|
|
|
|
|
|
storePageNoOnUrl: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: true
|
2022-03-21 16:16:43 +08:00
|
|
|
|
}
|
2021-06-11 16:59:16 +08:00
|
|
|
|
},
|
2022-09-13 16:54:41 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 添加vue3的setup,目的是添加/获取地址栏的参数
|
|
|
|
|
|
*/
|
2023-03-02 20:37:21 +08:00
|
|
|
|
setup (props) {
|
2022-09-13 16:54:41 +08:00
|
|
|
|
const { query } = useRoute()
|
2023-10-14 15:22:38 +08:00
|
|
|
|
// pageSize取值顺序:1.url;2.缓存;3.pageObj;4.默认值
|
|
|
|
|
|
const urlPageSize = parseInt(query.pageSize)
|
|
|
|
|
|
const cachePageSize = parseInt(localStorage.getItem(storageKey.pageSize + '-' + localStorage.getItem(storageKey.username) + '-' + props.tableId))
|
|
|
|
|
|
const pageObjPageSize = props.pageObj.pageSize
|
|
|
|
|
|
const pageSize = ref(urlPageSize || cachePageSize || pageObjPageSize || defaultPageSize)
|
2023-03-02 20:37:21 +08:00
|
|
|
|
const currentPageNo = ref(props.storePageNoOnUrl ? (query.pageNo || (props.pageObj.pageNo || 1)) : (props.pageObj.pageNo || 1))
|
2022-09-13 16:54:41 +08:00
|
|
|
|
return {
|
|
|
|
|
|
pageSize,
|
|
|
|
|
|
currentPageNo
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2023-10-14 15:22:38 +08:00
|
|
|
|
mounted () {
|
|
|
|
|
|
if (this.postPageSizes && this.postPageSizes.length > 0) {
|
|
|
|
|
|
this.resetPageSizes()
|
|
|
|
|
|
}
|
|
|
|
|
|
this.currentPageNo = parseInt(this.currentPageNo)
|
|
|
|
|
|
this.current(this.currentPageNo)
|
|
|
|
|
|
},
|
2021-06-11 16:59:16 +08:00
|
|
|
|
data () {
|
|
|
|
|
|
return {
|
2022-09-13 16:54:41 +08:00
|
|
|
|
// pageSize: defaultPageSize,
|
2021-06-11 16:59:16 +08:00
|
|
|
|
pageSizes: [
|
|
|
|
|
|
{
|
|
|
|
|
|
label: '20' + this.$t('pageSize'),
|
|
|
|
|
|
value: 20
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
label: '50' + this.$t('pageSize'),
|
|
|
|
|
|
value: 50
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
label: '100' + this.$t('pageSize'),
|
|
|
|
|
|
value: 100
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
2022-09-09 19:35:58 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 向地址栏添加/删除参数
|
|
|
|
|
|
*/
|
|
|
|
|
|
reloadUrl (newParam, clean) {
|
2022-09-15 15:12:33 +08:00
|
|
|
|
const { query } = this.$route
|
|
|
|
|
|
let newUrl = urlParamsHandler(window.location.href, query, newParam)
|
|
|
|
|
|
if (clean) {
|
|
|
|
|
|
newUrl = urlParamsHandler(window.location.href, query, newParam, clean)
|
2022-09-09 19:35:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
overwriteUrl(newUrl)
|
|
|
|
|
|
},
|
2021-06-11 16:59:16 +08:00
|
|
|
|
popperVisible: function (visible) {
|
|
|
|
|
|
if (visible == true) {
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
|
if (this.popClass && this.popClass === 'out-popper-fix') {
|
|
|
|
|
|
const popDoms = document.querySelectorAll('.out-popper-fix')
|
|
|
|
|
|
popDoms.forEach(item => {
|
|
|
|
|
|
item.style.bottom = item.style.top
|
|
|
|
|
|
item.style.top = 'unset'
|
|
|
|
|
|
|
|
|
|
|
|
const icon = item.querySelector('.popper__arrow')
|
|
|
|
|
|
if (icon) {
|
|
|
|
|
|
icon.style.bottom = icon.style.top
|
|
|
|
|
|
icon.style.top = 'unset'
|
|
|
|
|
|
icon.style.transform = 'rotate(180deg)'
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
background () {
|
|
|
|
|
|
this.backgroundColor()
|
|
|
|
|
|
},
|
|
|
|
|
|
// 点击上一页箭头
|
|
|
|
|
|
prev () {
|
|
|
|
|
|
this.backgroundColor()
|
|
|
|
|
|
this.scrollbarToTop()
|
|
|
|
|
|
},
|
|
|
|
|
|
// 点击下一页箭头
|
|
|
|
|
|
next (val) {
|
|
|
|
|
|
this.backgroundColor()
|
|
|
|
|
|
this.scrollbarToTop()
|
|
|
|
|
|
},
|
|
|
|
|
|
// currentPage 改变时会触发
|
|
|
|
|
|
current (val) {
|
|
|
|
|
|
this.$emit('pageNo', val)
|
|
|
|
|
|
this.backgroundColor()
|
|
|
|
|
|
this.scrollbarToTop()
|
2022-09-13 16:54:41 +08:00
|
|
|
|
// this.currentPageNo = val;
|
2022-09-09 19:35:58 +08:00
|
|
|
|
|
2022-09-15 15:12:33 +08:00
|
|
|
|
const newParam = {
|
2022-09-13 16:54:41 +08:00
|
|
|
|
pageNo: val
|
2022-09-09 19:35:58 +08:00
|
|
|
|
}
|
2022-09-15 15:12:33 +08:00
|
|
|
|
this.reloadUrl(newParam)
|
2021-06-11 16:59:16 +08:00
|
|
|
|
},
|
2022-09-09 19:35:58 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更改页码大小
|
|
|
|
|
|
*/
|
2021-06-11 16:59:16 +08:00
|
|
|
|
size (val) {
|
|
|
|
|
|
// eslint-disable-next-line vue/no-mutating-props
|
2022-09-13 16:54:41 +08:00
|
|
|
|
// this.pageObj.pageNo = 1
|
2023-10-14 15:22:38 +08:00
|
|
|
|
this.$emit('scrollbarToTop')
|
2021-06-11 16:59:16 +08:00
|
|
|
|
this.$emit('pageSize', val)
|
|
|
|
|
|
this.backgroundColor()
|
2022-09-09 19:35:58 +08:00
|
|
|
|
|
2022-09-15 15:12:33 +08:00
|
|
|
|
const newParam = {
|
2022-09-09 19:35:58 +08:00
|
|
|
|
pageSize: val
|
|
|
|
|
|
}
|
2022-09-15 15:12:33 +08:00
|
|
|
|
this.reloadUrl(newParam)
|
2021-06-11 16:59:16 +08:00
|
|
|
|
},
|
|
|
|
|
|
scrollbarToTop () {
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
|
const wraps = document.querySelectorAll('.el-table__body-wrapper')
|
|
|
|
|
|
wraps.forEach(wrap => {
|
|
|
|
|
|
if (wrap._ps_) {
|
|
|
|
|
|
wrap.scrollTop = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2023-10-14 15:22:38 +08:00
|
|
|
|
this.$emit('scrollbarToTop')
|
2021-06-11 16:59:16 +08:00
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
resetPageSizes: function () {
|
|
|
|
|
|
if (this.postPageSizes) {
|
2023-10-14 15:22:38 +08:00
|
|
|
|
this.pageSizes = this.postPageSizes.map(item => {
|
2022-09-15 15:12:33 +08:00
|
|
|
|
return {
|
|
|
|
|
|
label: item + this.$t('pageSize'),
|
|
|
|
|
|
value: item
|
|
|
|
|
|
}
|
2021-06-11 16:59:16 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
// 设置当前页的样式
|
|
|
|
|
|
backgroundColor () {
|
2022-09-09 19:35:58 +08:00
|
|
|
|
// this.list = this.$refs.page.$el.children[2].children
|
|
|
|
|
|
// for (let i = 0; i < this.list.length; i++) {
|
|
|
|
|
|
// // const element = this.list[i]
|
|
|
|
|
|
// }
|
2021-06-11 16:59:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
watch: {
|
|
|
|
|
|
postPageSizes: {
|
|
|
|
|
|
immediate: true,
|
|
|
|
|
|
deep: true,
|
|
|
|
|
|
handler (n, o) {
|
|
|
|
|
|
this.resetPageSizes()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
popClass: {
|
|
|
|
|
|
immediate: true,
|
|
|
|
|
|
handler (n, o) {
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
pageObj: {
|
|
|
|
|
|
immediate: true,
|
|
|
|
|
|
deep: true,
|
|
|
|
|
|
handler (n, o) {
|
|
|
|
|
|
}
|
2022-04-12 18:00:01 +08:00
|
|
|
|
},
|
|
|
|
|
|
tableData: {
|
|
|
|
|
|
immediate: true,
|
|
|
|
|
|
deep: true,
|
|
|
|
|
|
handler (n, o) {
|
|
|
|
|
|
if (n) {
|
|
|
|
|
|
this.checkbox = n
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-06-11 16:59:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|