index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div :class="{'hidden':hidden}"
  3. class="pagination-container">
  4. <!-- :background="background" -->
  5. <el-pagination :current-page.sync="currentPage"
  6. :page-size.sync="pageSize"
  7. :layout="layout"
  8. :page-sizes="pageSizes"
  9. :total="total"
  10. v-bind="$attrs"
  11. @size-change="handleSizeChange"
  12. @current-change="handleCurrentChange" />
  13. </div>
  14. </template>
  15. <script>
  16. import { scrollTo } from '@/utils/scroll-to'
  17. import { Searchs } from '@/helpers'
  18. export default {
  19. name: 'Pagination',
  20. props: {
  21. total: {
  22. required: true,
  23. type: Number
  24. },
  25. page: {
  26. type: Number,
  27. default: 1
  28. },
  29. limit: {
  30. type: Number,
  31. default: 10
  32. },
  33. pageSizes: {
  34. type: Array,
  35. default () {
  36. return [10, 20, 30, 50]
  37. }
  38. },
  39. layout: {
  40. type: String,
  41. default: 'total,sizes,prev, pager, next, jumper'
  42. },
  43. background: {
  44. type: Boolean,
  45. default: true
  46. },
  47. autoScroll: {
  48. type: Boolean,
  49. default: true
  50. },
  51. hidden: {
  52. type: Boolean,
  53. default: false
  54. },
  55. sync: {
  56. type: Boolean,
  57. default: false
  58. }
  59. },
  60. computed: {
  61. currentPage: {
  62. get () {
  63. return this.page
  64. },
  65. set (val) {
  66. this.$emit('update:page', val)
  67. }
  68. },
  69. pageSize: {
  70. get () {
  71. return this.limit
  72. },
  73. set (val) {
  74. this.$emit('update:limit', val)
  75. }
  76. }
  77. },
  78. mounted() {
  79. if (this.sync) {
  80. const searchs = new Searchs(this.$route.path)
  81. const active = searchs.get()
  82. if (active && active.page) {
  83. for (const key in active.page) {
  84. if (active.page.hasOwnProperty(key)) {
  85. const item = active.page[key]
  86. this.$emit('update:' + key, item)
  87. }
  88. }
  89. }
  90. }
  91. },
  92. methods: {
  93. syncStore() {
  94. if (this.sync) {
  95. const searchs = new Searchs(this.$route.path)
  96. searchs.update(this._props, undefined, 'page')
  97. }
  98. },
  99. handleSizeChange (val) {
  100. this.$emit('pagination', { page: this.currentPage, limit: val })
  101. if (this.autoScroll) {
  102. scrollTo(0, 800)
  103. }
  104. this.syncStore()
  105. },
  106. handleCurrentChange (val) {
  107. this.$emit('pagination', { page: val, limit: this.pageSize })
  108. if (this.autoScroll) {
  109. scrollTo(0, 800)
  110. }
  111. this.syncStore()
  112. }
  113. }
  114. }
  115. </script>
  116. <style scoped>
  117. .pagination-container {
  118. background: #fff;
  119. padding: 32px 16px;
  120. width: 100%;
  121. display: flex;
  122. flex-direction: row;
  123. justify-content: center;
  124. }
  125. .pagination-container.hidden {
  126. display: none;
  127. }
  128. </style>