index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. saveKey: {
  60. type: String,
  61. default: ''
  62. }
  63. },
  64. data() {
  65. return {
  66. pageInformation: null
  67. }
  68. },
  69. computed: {
  70. currentPage: {
  71. get () {
  72. return this.page
  73. },
  74. set (val) {
  75. this.$emit('update:page', val)
  76. }
  77. },
  78. pageSize: {
  79. get () {
  80. return this.limit
  81. },
  82. set (val) {
  83. this.$emit('update:limit', val)
  84. }
  85. }
  86. },
  87. watch: {
  88. currentPage() {
  89. this.syncStore()
  90. },
  91. pageSize() {
  92. this.syncStore()
  93. }
  94. },
  95. mounted() {
  96. if (this.sync) {
  97. const searchs = new Searchs(this.saveKey || this.$route.path)
  98. const active = searchs.get()
  99. this.pageInformation = active
  100. if (active && active.page) {
  101. for (const key in active.page) {
  102. if (active.page.hasOwnProperty(key)) {
  103. const item = active.page[key]
  104. this.$emit('update:' + key, item)
  105. }
  106. }
  107. }
  108. if (this.saveKey) {
  109. searchs.update(this.$route.path, undefined, 'bind')
  110. }
  111. }
  112. window.addEventListener('watchStorage', this.watchStorage)
  113. },
  114. methods: {
  115. watchStorage() {
  116. let page = this.pageInformation && this.pageInformation.page ? this.pageInformation.page : null
  117. this.currentPage = page && page.page ? page.page : 1
  118. },
  119. syncStore() {
  120. if (this.sync) {
  121. const searchs = new Searchs(this.saveKey || this.$route.path)
  122. searchs.update(this._props, undefined, 'page')
  123. }
  124. },
  125. handleSizeChange (val) {
  126. this.currentPage = 1
  127. this.$emit('pagination', { page: this.currentPage, limit: val })
  128. if (this.autoScroll) {
  129. scrollTo(0, 800)
  130. }
  131. this.syncStore()
  132. },
  133. handleCurrentChange (val) {
  134. this.$emit('pagination', { page: val, limit: this.pageSize })
  135. if (this.autoScroll) {
  136. scrollTo(0, 800)
  137. }
  138. this.syncStore()
  139. }
  140. },
  141. destroyed() {
  142. window.removeEventListener('watchStorage', this.watchStorage)
  143. }
  144. }
  145. </script>
  146. <style scoped>
  147. .pagination-container {
  148. background: #fff;
  149. padding: 32px 16px;
  150. width: 100%;
  151. display: flex;
  152. flex-direction: row;
  153. justify-content: center;
  154. }
  155. .pagination-container.hidden {
  156. display: none;
  157. }
  158. </style>