天波用户运营管理后台系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.4KB

  1. <template>
  2. <div :class="{'hidden':hidden}" class="pagination-container">
  3. <el-row class="pagination-row">
  4. <el-col :xs="20" :sm="20" :md="20" :lg="16" :xl="12">
  5. <el-pagination
  6. :background="background"
  7. :current-page.sync="currentPage"
  8. :page-size.sync="pageSize"
  9. :layout="layout"
  10. :page-sizes="pageSizes"
  11. small
  12. ref="pagination"
  13. :total="total"
  14. v-bind="$attrs"
  15. @size-change="handleSizeChange"
  16. @current-change="handleCurrentChange"
  17. />
  18. </el-col>
  19. <el-col :span="4" class="jump-button" v-show="isMobile">
  20. <el-button type="primary" @click="onJump">跳转</el-button>
  21. </el-col>
  22. </el-row>
  23. </div>
  24. </template>
  25. <script>
  26. import { scrollTo } from "@/utils/scroll-to";
  27. /* const { body } = document; */
  28. export default {
  29. name: "Pagination",
  30. data() {
  31. return {
  32. screenWidth: document.body.clientWidth,
  33. /* isMobile: false */
  34. };
  35. },
  36. props: {
  37. total: {
  38. required: true,
  39. type: Number
  40. },
  41. page: {
  42. type: Number,
  43. default: 1
  44. },
  45. limit: {
  46. type: Number,
  47. default: 20
  48. },
  49. pageSizes: {
  50. type: Array,
  51. default() {
  52. return [10, 20, 30, 50];
  53. }
  54. },
  55. layout: {
  56. type: String,
  57. default: "total, sizes, prev, pager, next, jumper" //total, sizes, prev, pager, next, jumper
  58. },
  59. background: {
  60. type: Boolean,
  61. default: true
  62. },
  63. autoScroll: {
  64. type: Boolean,
  65. default: true
  66. },
  67. hidden: {
  68. type: Boolean,
  69. default: false
  70. }
  71. },
  72. computed: {
  73. currentPage: {
  74. get() {
  75. return this.page;
  76. },
  77. set(val) {
  78. this.$emit("update:page", val);
  79. }
  80. },
  81. pageSize: {
  82. get() {
  83. return this.limit;
  84. },
  85. set(val) {
  86. this.$emit("update:limit", val);
  87. }
  88. },
  89. // 防止在移动端刷新按钮消失
  90. isMobile: {
  91. get() {
  92. if(this.screenWidth < 991) {
  93. return true
  94. } else {
  95. return false
  96. }
  97. },
  98. set() {
  99. this.screenWidth
  100. }
  101. }
  102. },
  103. watch: {
  104. // 监听浏览器可视窗口变化
  105. screenWidth(val){
  106. const IWIDTH = 991;
  107. if(val - 1 < IWIDTH) {
  108. this.isMobile = true;
  109. } else {
  110. this.isMobile = false;
  111. }
  112. }
  113. },
  114. mounted() {
  115. this.getScreenWidth();
  116. },
  117. methods: {
  118. handleSizeChange(val) {
  119. this.$emit("pagination", { page: this.currentPage, limit: val });
  120. if (this.autoScroll) {
  121. scrollTo(0, 800);
  122. }
  123. },
  124. handleCurrentChange(val) {
  125. this.$emit("pagination", { page: val, limit: this.pageSize });
  126. if (this.autoScroll) {
  127. scrollTo(0, 800);
  128. }
  129. },
  130. // 跳转确认
  131. onJump() {
  132. let evtObj;
  133. let elevatorDiv = document.getElementsByClassName(
  134. "el-pagination__editor"
  135. );
  136. if (elevatorDiv && elevatorDiv.length > 0) {
  137. let pageInput = elevatorDiv[0].getElementsByTagName("input")[0];
  138. if (window.KeyEvent) {
  139. //firefox 浏览器下模拟事件
  140. evtObj = document.createEvent("KeyEvents");
  141. evtObj.initKeyEvent(
  142. "keyup",
  143. true,
  144. true,
  145. window,
  146. true,
  147. false,
  148. false,
  149. false,
  150. 13,
  151. 0
  152. );
  153. } else {
  154. //chrome 浏览器下模拟事件
  155. evtObj = document.createEvent("UIEvents");
  156. evtObj.initUIEvent("keyup", true, true, window, 1);
  157. delete evtObj.keyCode;
  158. if (typeof evtObj.keyCode === "undefined") {
  159. //为了模拟keycode
  160. Object.defineProperty(evtObj, "keyCode", { value: 13 });
  161. } else {
  162. evtObj.key = String.fromCharCode(13);
  163. }
  164. }
  165. pageInput.dispatchEvent(evtObj);
  166. }
  167. },
  168. // 获取浏览器可视化宽度
  169. getScreenWidth() {
  170. let that = this;
  171. window.onresize = () => {
  172. return (() => {
  173. window.screenWidth = document.body.clientWidth;
  174. that.screenWidth = window.screenWidth;
  175. })();
  176. };
  177. }
  178. }
  179. };
  180. </script>
  181. <style scoped lang="scss">
  182. .pagination-container {
  183. width: 100%;
  184. background: #fff;
  185. padding: 32px 16px;
  186. }
  187. .pagination-container.hidden {
  188. display: none;
  189. }
  190. </style>