index.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!-- 基于 ruoyi-vue3 的 Pagination 重构,核心是简化无用的属性,并使用 ts 重写 -->
  2. <template>
  3. <el-pagination
  4. v-show="total > 0"
  5. v-model:current-page="currentPage"
  6. v-model:page-size="pageSize"
  7. :background="true"
  8. :page-sizes="[10, 20, 30, 50, 100]"
  9. :pager-count="pagerCount"
  10. :total="total"
  11. :size="isSmall ? 'small' : 'default'"
  12. class="float-right mb-15px mt-15px"
  13. layout="total, sizes, prev, pager, next, jumper"
  14. @size-change="handleSizeChange"
  15. @current-change="handleCurrentChange" />
  16. </template>
  17. <script lang="ts" setup>
  18. import { computed, watchEffect } from 'vue'
  19. import { useAppStore } from '@/store/modules/app'
  20. defineOptions({ name: 'Pagination' })
  21. // 此处解决了当全局size为small的时候分页组件样式太大的问题
  22. const appStore = useAppStore()
  23. const layoutCurrentSize = computed(() => appStore.currentSize)
  24. const isSmall = ref<boolean>(layoutCurrentSize.value === 'small')
  25. watchEffect(() => {
  26. isSmall.value = layoutCurrentSize.value === 'small'
  27. })
  28. const props = defineProps({
  29. // 总条目数
  30. total: {
  31. required: true,
  32. type: Number
  33. },
  34. // 当前页数:pageNo
  35. page: {
  36. type: Number,
  37. default: 1
  38. },
  39. // 每页显示条目个数:pageSize
  40. limit: {
  41. type: Number,
  42. default: 20
  43. },
  44. // 设置最大页码按钮数。 页码按钮的数量,当总页数超过该值时会折叠
  45. // 移动端页码按钮的数量端默认值 5
  46. pagerCount: {
  47. type: Number,
  48. default: document.body.clientWidth < 992 ? 5 : 7
  49. }
  50. })
  51. const emit = defineEmits(['update:page', 'update:limit', 'pagination'])
  52. const currentPage = computed({
  53. get() {
  54. return props.page
  55. },
  56. set(val) {
  57. // 触发 update:page 事件,更新 limit 属性,从而更新 pageNo
  58. emit('update:page', val)
  59. }
  60. })
  61. const pageSize = computed({
  62. get() {
  63. return props.limit
  64. },
  65. set(val) {
  66. // 触发 update:limit 事件,更新 limit 属性,从而更新 pageSize
  67. emit('update:limit', val)
  68. }
  69. })
  70. const handleSizeChange = (val) => {
  71. // 如果修改后超过最大页面,强制跳转到第 1 页
  72. if (currentPage.value * val > props.total) {
  73. currentPage.value = 1
  74. }
  75. // 触发 pagination 事件,重新加载列表
  76. emit('pagination', { page: currentPage.value, limit: val })
  77. }
  78. const handleCurrentChange = (val) => {
  79. // 触发 pagination 事件,重新加载列表
  80. emit('pagination', { page: val, limit: pageSize.value })
  81. }
  82. </script>