2023-03-11 00:11:48 +08:00
|
|
|
|
<!-- 基于 ruoyi-vue3 的 Pagination 重构,核心是简化无用的属性,并使用 ts 重写 -->
|
2023-03-08 09:49:14 +08:00
|
|
|
|
<template>
|
2023-03-11 00:11:48 +08:00
|
|
|
|
<el-pagination
|
|
|
|
|
v-show="total > 0"
|
|
|
|
|
v-model:current-page="currentPage"
|
|
|
|
|
v-model:page-size="pageSize"
|
2023-04-14 21:32:11 +08:00
|
|
|
|
:background="true"
|
|
|
|
|
:page-sizes="[10, 20, 30, 50, 100]"
|
2023-03-11 00:11:48 +08:00
|
|
|
|
:pager-count="pagerCount"
|
|
|
|
|
:total="total"
|
2023-08-23 13:51:24 +08:00
|
|
|
|
:small="isSmall"
|
2023-09-22 17:49:11 +08:00
|
|
|
|
class="float-right mb-15px mt-15px"
|
2023-04-14 21:32:11 +08:00
|
|
|
|
layout="total, sizes, prev, pager, next, jumper"
|
2023-03-11 00:11:48 +08:00
|
|
|
|
@size-change="handleSizeChange"
|
|
|
|
|
@current-change="handleCurrentChange"
|
|
|
|
|
/>
|
2023-03-08 09:49:14 +08:00
|
|
|
|
</template>
|
2023-06-21 19:35:11 +08:00
|
|
|
|
<script lang="ts" setup>
|
2023-08-23 13:51:24 +08:00
|
|
|
|
import { computed, watchEffect } from 'vue'
|
|
|
|
|
import { useAppStore } from '@/store/modules/app'
|
2023-03-08 09:49:14 +08:00
|
|
|
|
|
2023-06-21 19:14:34 +08:00
|
|
|
|
defineOptions({ name: 'Pagination' })
|
|
|
|
|
|
2023-08-23 13:51:24 +08:00
|
|
|
|
// 此处解决了当全局size为small的时候分页组件样式太大的问题
|
|
|
|
|
const appStore = useAppStore()
|
|
|
|
|
const layoutCurrentSize = computed(() => appStore.currentSize)
|
|
|
|
|
const isSmall = ref<boolean>(layoutCurrentSize.value === 'small')
|
|
|
|
|
watchEffect(() => {
|
|
|
|
|
isSmall.value = layoutCurrentSize.value === 'small'
|
|
|
|
|
})
|
|
|
|
|
|
2023-03-08 09:49:14 +08:00
|
|
|
|
const props = defineProps({
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 总条目数
|
2023-03-08 09:49:14 +08:00
|
|
|
|
total: {
|
|
|
|
|
required: true,
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 当前页数:pageNo
|
2023-03-08 09:49:14 +08:00
|
|
|
|
page: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1
|
|
|
|
|
},
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 每页显示条目个数:pageSize
|
2023-03-08 09:49:14 +08:00
|
|
|
|
limit: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 20
|
|
|
|
|
},
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 设置最大页码按钮数。 页码按钮的数量,当总页数超过该值时会折叠
|
2023-03-10 23:09:30 +08:00
|
|
|
|
// 移动端页码按钮的数量端默认值 5
|
2023-03-08 09:49:14 +08:00
|
|
|
|
pagerCount: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: document.body.clientWidth < 992 ? 5 : 7
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2023-03-10 23:09:30 +08:00
|
|
|
|
const emit = defineEmits(['update:page', 'update:limit', 'pagination', 'pagination'])
|
2023-03-08 09:49:14 +08:00
|
|
|
|
const currentPage = computed({
|
|
|
|
|
get() {
|
|
|
|
|
return props.page
|
|
|
|
|
},
|
|
|
|
|
set(val) {
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 触发 update:page 事件,更新 limit 属性,从而更新 pageNo
|
2023-03-08 09:49:14 +08:00
|
|
|
|
emit('update:page', val)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
const pageSize = computed({
|
|
|
|
|
get() {
|
|
|
|
|
return props.limit
|
|
|
|
|
},
|
2023-03-10 23:09:30 +08:00
|
|
|
|
set(val) {
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 触发 update:limit 事件,更新 limit 属性,从而更新 pageSize
|
2023-03-08 09:49:14 +08:00
|
|
|
|
emit('update:limit', val)
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-03-11 00:11:48 +08:00
|
|
|
|
const handleSizeChange = (val) => {
|
|
|
|
|
// 如果修改后超过最大页面,强制跳转到第 1 页
|
2023-03-08 09:49:14 +08:00
|
|
|
|
if (currentPage.value * val > props.total) {
|
|
|
|
|
currentPage.value = 1
|
|
|
|
|
}
|
2023-03-11 00:11:48 +08:00
|
|
|
|
// 触发 pagination 事件,重新加载列表
|
2023-03-08 09:49:14 +08:00
|
|
|
|
emit('pagination', { page: currentPage.value, limit: val })
|
|
|
|
|
}
|
2023-03-11 00:11:48 +08:00
|
|
|
|
const handleCurrentChange = (val) => {
|
|
|
|
|
// 触发 pagination 事件,重新加载列表
|
2023-03-08 09:49:14 +08:00
|
|
|
|
emit('pagination', { page: val, limit: pageSize.value })
|
|
|
|
|
}
|
|
|
|
|
</script>
|