index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 lang="ts">
  17. export default {
  18. name: 'Pagination'
  19. }
  20. </script>
  21. <script setup lang="ts">
  22. import { scrollTo } from '@/utils/scroll-to'
  23. import { PropType } from "vue";
  24. const props = defineProps({
  25. total: {
  26. required: true,
  27. type: Number
  28. },
  29. page: {
  30. type: Number,
  31. default: 1
  32. },
  33. limit: {
  34. type: Number,
  35. default: 20
  36. },
  37. pageSizes: {
  38. type: Array as PropType<number[]>,
  39. default() {
  40. return [10, 20, 30, 50]
  41. }
  42. },
  43. // 移动端页码按钮的数量端默认值5
  44. pagerCount: {
  45. type: Number,
  46. default: document.body.clientWidth < 992 ? 5 : 7
  47. },
  48. layout: {
  49. type: String,
  50. default: 'total, sizes, prev, pager, next, jumper'
  51. },
  52. background: {
  53. type: Boolean,
  54. default: true
  55. },
  56. autoScroll: {
  57. type: Boolean,
  58. default: true
  59. },
  60. hidden: {
  61. type: Boolean,
  62. default: false
  63. },
  64. float: {
  65. type: String,
  66. default: 'right'
  67. }
  68. })
  69. const emit = defineEmits(['update:page', 'update:limit', 'pagination']);
  70. const currentPage = computed({
  71. get() {
  72. return props.page
  73. },
  74. set(val) {
  75. emit('update:page', val)
  76. }
  77. })
  78. const pageSize = computed({
  79. get() {
  80. return props.limit
  81. },
  82. set(val){
  83. emit('update:limit', val)
  84. }
  85. })
  86. function handleSizeChange(val: number) {
  87. if (currentPage.value * val > props.total) {
  88. currentPage.value = 1
  89. }
  90. emit('pagination', { page: currentPage.value, limit: val })
  91. if (props.autoScroll) {
  92. scrollTo(0, 800)
  93. }
  94. }
  95. function handleCurrentChange(val: number) {
  96. emit('pagination', { page: val, limit: pageSize.value })
  97. if (props.autoScroll) {
  98. scrollTo(0, 800)
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .pagination-container {
  104. background: #fff;
  105. padding: 32px 16px;
  106. .el-pagination{
  107. float: v-bind(float);
  108. }
  109. }
  110. .pagination-container.hidden {
  111. display: none;
  112. }
  113. </style>