index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div :class="{ hidden: hidden }" class="pagination-container">
  3. <!-- :background="background" -->
  4. <el-pagination
  5. :current-page.sync="currentPage"
  6. size="mini"
  7. :page-size.sync="pageSize"
  8. :layout="layout"
  9. :page-sizes="pageSizes"
  10. :total="total"
  11. v-bind="$attrs"
  12. @size-change="handleSizeChange"
  13. @current-change="handleCurrentChange"
  14. />
  15. </div>
  16. </template>
  17. <script>
  18. import { scrollTo } from "@/utils/scroll-to";
  19. import { Searchs } from "@/helpers";
  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: 10,
  34. },
  35. pageSizes: {
  36. type: Array,
  37. default() {
  38. return [10, 20, 30, 50];
  39. },
  40. },
  41. layout: {
  42. type: String,
  43. default: "total,sizes,prev, pager, next, jumper",
  44. },
  45. background: {
  46. type: Boolean,
  47. default: true,
  48. },
  49. autoScroll: {
  50. type: Boolean,
  51. default: true,
  52. },
  53. hidden: {
  54. type: Boolean,
  55. default: false,
  56. },
  57. sync: {
  58. type: Boolean,
  59. default: false,
  60. },
  61. saveKey: {
  62. type: String,
  63. default: "",
  64. },
  65. },
  66. data() {
  67. return {
  68. pageInformation: null,
  69. };
  70. },
  71. computed: {
  72. currentPage: {
  73. get() {
  74. return this.page;
  75. },
  76. set(val) {
  77. this.$emit("update:page", val);
  78. },
  79. },
  80. pageSize: {
  81. get() {
  82. return this.limit;
  83. },
  84. set(val) {
  85. this.$emit("update:limit", val);
  86. },
  87. },
  88. },
  89. watch: {
  90. currentPage() {
  91. this.syncStore();
  92. },
  93. pageSize() {
  94. this.syncStore();
  95. },
  96. },
  97. mounted() {
  98. if (this.sync) {
  99. const searchs = new Searchs(this.saveKey || this.$route.path);
  100. const active = searchs.get();
  101. this.pageInformation = active;
  102. if (active && active.page) {
  103. for (const key in active.page) {
  104. if (active.page.hasOwnProperty(key)) {
  105. const item = active.page[key];
  106. this.$emit("update:" + key, item);
  107. }
  108. }
  109. }
  110. if (this.saveKey) {
  111. searchs.update(this.$route.path, undefined, "bind");
  112. }
  113. }
  114. window.addEventListener("watchStorage", this.watchStorage);
  115. },
  116. methods: {
  117. watchStorage() {
  118. let page =
  119. this.pageInformation && this.pageInformation.page
  120. ? this.pageInformation.page
  121. : null;
  122. this.currentPage = page && page.page ? page.page : 1;
  123. },
  124. syncStore() {
  125. if (this.sync) {
  126. const searchs = new Searchs(this.saveKey || this.$route.path);
  127. searchs.update(this._props, undefined, "page");
  128. }
  129. },
  130. handleSizeChange(val) {
  131. this.currentPage = 1;
  132. this.$emit("pagination", { page: this.currentPage, limit: val });
  133. if (this.autoScroll) {
  134. scrollTo(0, 800);
  135. }
  136. this.syncStore();
  137. },
  138. handleCurrentChange(val) {
  139. this.$emit("pagination", { page: val, limit: this.pageSize });
  140. if (this.autoScroll) {
  141. scrollTo(0, 800);
  142. }
  143. this.syncStore();
  144. },
  145. },
  146. destroyed() {
  147. window.removeEventListener("watchStorage", this.watchStorage);
  148. },
  149. };
  150. </script>
  151. <style scoped lang="scss">
  152. .pagination-container {
  153. background: #fff;
  154. padding: 32px 16px;
  155. width: 100%;
  156. display: flex;
  157. flex-direction: row;
  158. justify-content: center;
  159. }
  160. .pagination-container.hidden {
  161. display: none;
  162. }
  163. ::v-deep .el-select {
  164. width: 100px !important;
  165. margin: 0 5px;
  166. }
  167. </style>