index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. :small="isSmall"
  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. />
  17. </template>
  18. <script lang="ts" setup>
  19. import { computed, watchEffect } from 'vue'
  20. import { useAppStore } from '@/store/modules/app'
  21. defineOptions({ name: 'Pagination' })
  22. // 此处解决了当全局size为small的时候分页组件样式太大的问题
  23. const appStore = useAppStore()
  24. const layoutCurrentSize = computed(() => appStore.currentSize)
  25. const isSmall = ref<boolean>(layoutCurrentSize.value === 'small')
  26. watchEffect(() => {
  27. isSmall.value = layoutCurrentSize.value === 'small'
  28. })
  29. const props = defineProps({
  30. // 总条目数
  31. total: {
  32. required: true,
  33. type: [Number, String]
  34. },
  35. // 当前页数:pageNo
  36. page: {
  37. type: Number,
  38. default: 1
  39. },
  40. // 每页显示条目个数:pageSize
  41. limit: {
  42. type: Number,
  43. default: 20
  44. },
  45. // 设置最大页码按钮数。 页码按钮的数量,当总页数超过该值时会折叠
  46. // 移动端页码按钮的数量端默认值 5
  47. pagerCount: {
  48. type: Number,
  49. default: document.body.clientWidth < 992 ? 5 : 7
  50. }
  51. })
  52. const emit = defineEmits(['update:page', 'update:limit', 'pagination'])
  53. const currentPage = computed({
  54. get() {
  55. return props.page
  56. },
  57. set(val) {
  58. // 触发 update:page 事件,更新 limit 属性,从而更新 pageNo
  59. emit('update:page', val)
  60. }
  61. })
  62. const pageSize = computed({
  63. get() {
  64. return props.limit
  65. },
  66. set(val) {
  67. // 触发 update:limit 事件,更新 limit 属性,从而更新 pageSize
  68. emit('update:limit', val)
  69. }
  70. })
  71. const handleSizeChange = (val) => {
  72. // 如果修改后超过最大页面,强制跳转到第 1 页
  73. if (currentPage.value * val > +props.total) {
  74. currentPage.value = 1
  75. }
  76. // 触发 pagination 事件,重新加载列表
  77. emit('pagination', { page: currentPage.value, limit: val })
  78. }
  79. const handleCurrentChange = (val) => {
  80. // 触发 pagination 事件,重新加载列表
  81. emit('pagination', { page: val, limit: pageSize.value })
  82. }
  83. </script>