index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div :class="{ 'hidden': hidden }" class="pagination-container">
  3. <el-pagination
  4. :background="background"
  5. v-model:current-page="currentPage"
  6. v-model:page-size="pageSize"
  7. :layout="layout"
  8. :page-sizes="pageSizes"
  9. :pager-count="pagerCount"
  10. :total="total"
  11. @size-change="handleSizeChange"
  12. @current-change="handleCurrentChange"
  13. />
  14. </div>
  15. </template>
  16. <script setup>
  17. import { scrollTo } from '@/utils/scroll-to'
  18. const props = defineProps({
  19. total: {
  20. required: true,
  21. type: Number
  22. },
  23. page: {
  24. type: Number,
  25. default: 1
  26. },
  27. limit: {
  28. type: Number,
  29. default: 20
  30. },
  31. pageSizes: {
  32. type: Array,
  33. default() {
  34. return [10, 20, 30, 50]
  35. }
  36. },
  37. // 移动端页码按钮的数量端默认值5
  38. pagerCount: {
  39. type: Number,
  40. default: document.body.clientWidth < 992 ? 5 : 7
  41. },
  42. layout: {
  43. type: String,
  44. default: 'total, sizes, prev, pager, next, jumper'
  45. },
  46. background: {
  47. type: Boolean,
  48. default: true
  49. },
  50. autoScroll: {
  51. type: Boolean,
  52. default: true
  53. },
  54. hidden: {
  55. type: Boolean,
  56. default: false
  57. }
  58. })
  59. const emit = defineEmits();
  60. const currentPage = computed({
  61. get() {
  62. return props.page
  63. },
  64. set(val) {
  65. emit('update:page', val)
  66. }
  67. })
  68. const pageSize = computed({
  69. get() {
  70. return props.limit
  71. },
  72. set(val){
  73. emit('update:limit', val)
  74. }
  75. })
  76. function handleSizeChange(val) {
  77. if (currentPage.value * val > props.total) {
  78. currentPage.value = 1
  79. }
  80. emit('pagination', { page: currentPage.value, limit: val })
  81. if (props.autoScroll) {
  82. scrollTo(0, 800)
  83. }
  84. }
  85. function handleCurrentChange(val) {
  86. emit('pagination', { page: val, limit: pageSize.value })
  87. if (props.autoScroll) {
  88. scrollTo(0, 800)
  89. }
  90. }
  91. </script>
  92. <style scoped>
  93. .pagination-container {
  94. background: #fff;
  95. }
  96. .pagination-container.hidden {
  97. display: none;
  98. }
  99. </style>