index.vue 2.4 KB

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