index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <div :class="{ hidden: hidden }" class="pagination-container">
  3. <el-pagination :background="background" v-model:current-page="currentPage" v-model:page-size="pageSize"
  4. :layout="layout" :page-sizes="pageSizes" :pager-count="pagerCount" :total="total" :small="small"
  5. @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  6. </div>
  7. </template>
  8. <script lang="ts" name="pagination" setup>
  9. import {computed, onMounted, ref, watch} from 'vue';
  10. import mittBus from '@/utils/mitt';
  11. // 定义父组件传过来的值
  12. const props = defineProps({
  13. total: {
  14. required: true,
  15. type: Number,
  16. },
  17. page: {
  18. type: Number,
  19. default: 1,
  20. },
  21. limit: {
  22. type: Number,
  23. default: 20,
  24. },
  25. pageSizes: {
  26. type: Array,
  27. default() {
  28. return [10, 20, 30, 50, 100]
  29. },
  30. },
  31. // 移动端页码按钮的数量端默认值5
  32. pagerCount: {
  33. type: Number,
  34. default: document.body.clientWidth < 992 ? 5 : 7,
  35. },
  36. layout: {
  37. type: String,
  38. default: 'total, sizes, prev, pager, next, jumper',
  39. },
  40. background: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. autoScroll: {
  45. type: Boolean,
  46. default: true,
  47. },
  48. hidden: {
  49. type: Boolean,
  50. default: false,
  51. },
  52. })
  53. // 定义子组件向父组件传值/事件
  54. const emit = defineEmits(['update:page', 'update:limit', 'pagination']);
  55. // 定义变量内容
  56. const small = ref(false);
  57. const currentPage = computed({
  58. get() {
  59. return props.page
  60. },
  61. set(val) {
  62. emit('update:page', val)
  63. },
  64. })
  65. const pageSize = computed({
  66. get() {
  67. return props.limit
  68. },
  69. set(val) {
  70. emit('update:limit', val)
  71. },
  72. })
  73. const handleSizeChange = (val: any) => {
  74. /**
  75. * 场景:API返回总数为25条,按照10条每页,一共有3页。选了2的页数之后,然后把size选择成30条,
  76. * 这个时候,分页就会同时触发size选择函数以及current选择函数。{page: 2, size: 30},{page: 1, size: 30}请求两次,会导致列表会有暂无数据的情况
  77. * 解决办法:用setTimeout(函数,0),用延迟,虽然还是两次请求,但是每次都是{page: 1, size: 30}
  78. */
  79. setTimeout(() => {
  80. emit('pagination', { page: currentPage.value, limit: val })
  81. }, 0)
  82. mittBus.emit('scrollTopEmit', { top: 0 });// 分页发送监听事件 滚动到页面顶部
  83. }
  84. const handleCurrentChange = (val: any) => {
  85. emit('pagination', { page: val, limit: pageSize.value })
  86. mittBus.emit('scrollTopEmit', { top: 0 });// 分页发送监听事件 滚动到页面顶部
  87. }
  88. onMounted(() => {
  89. // 监听布局大小 改变分页的大小
  90. // let themeConfig = Local.get('themeConfig');
  91. // small.value = themeConfig.globalComponentSize == 'small';
  92. })
  93. </script>
  94. <style scoped>
  95. .pagination-container {
  96. padding-top: 20px;
  97. display: flex;
  98. justify-content: flex-end;
  99. }
  100. .pagination-container.hidden {
  101. display: none;
  102. }
  103. </style>