74b1415afb853cdfbffb29eecab62832d80fb99bfeb63833590b536bb30f7112f99da0887f35589397beab27822b6e4684a5daa9218bd1f094e620f36efc76 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <ul @click="onPagerClick" class="el-pager">
  3. <li
  4. :class="{ active: currentPage === 1, disabled }"
  5. v-if="pageCount > 0"
  6. class="number">1</li>
  7. <li
  8. class="el-icon more btn-quickprev"
  9. :class="[quickprevIconClass, { disabled }]"
  10. v-if="showPrevMore"
  11. @mouseenter="onMouseenter('left')"
  12. @mouseleave="quickprevIconClass = 'el-icon-more'">
  13. </li>
  14. <li
  15. v-for="pager in pagers"
  16. :key="pager"
  17. :class="{ active: currentPage === pager, disabled }"
  18. class="number">{{ pager }}</li>
  19. <li
  20. class="el-icon more btn-quicknext"
  21. :class="[quicknextIconClass, { disabled }]"
  22. v-if="showNextMore"
  23. @mouseenter="onMouseenter('right')"
  24. @mouseleave="quicknextIconClass = 'el-icon-more'">
  25. </li>
  26. <li
  27. :class="{ active: currentPage === pageCount, disabled }"
  28. class="number"
  29. v-if="pageCount > 1">{{ pageCount }}</li>
  30. </ul>
  31. </template>
  32. <script type="text/babel">
  33. export default {
  34. name: 'ElPager',
  35. props: {
  36. currentPage: Number,
  37. pageCount: Number,
  38. pagerCount: Number,
  39. disabled: Boolean
  40. },
  41. watch: {
  42. showPrevMore(val) {
  43. if (!val) this.quickprevIconClass = 'el-icon-more';
  44. },
  45. showNextMore(val) {
  46. if (!val) this.quicknextIconClass = 'el-icon-more';
  47. }
  48. },
  49. methods: {
  50. onPagerClick(event) {
  51. const target = event.target;
  52. if (target.tagName === 'UL' || this.disabled) {
  53. return;
  54. }
  55. let newPage = Number(event.target.textContent);
  56. const pageCount = this.pageCount;
  57. const currentPage = this.currentPage;
  58. const pagerCountOffset = this.pagerCount - 2;
  59. if (target.className.indexOf('more') !== -1) {
  60. if (target.className.indexOf('quickprev') !== -1) {
  61. newPage = currentPage - pagerCountOffset;
  62. } else if (target.className.indexOf('quicknext') !== -1) {
  63. newPage = currentPage + pagerCountOffset;
  64. }
  65. }
  66. /* istanbul ignore if */
  67. if (!isNaN(newPage)) {
  68. if (newPage < 1) {
  69. newPage = 1;
  70. }
  71. if (newPage > pageCount) {
  72. newPage = pageCount;
  73. }
  74. }
  75. if (newPage !== currentPage) {
  76. this.$emit('change', newPage);
  77. }
  78. },
  79. onMouseenter(direction) {
  80. if (this.disabled) return;
  81. if (direction === 'left') {
  82. this.quickprevIconClass = 'el-icon-d-arrow-left';
  83. } else {
  84. this.quicknextIconClass = 'el-icon-d-arrow-right';
  85. }
  86. }
  87. },
  88. computed: {
  89. pagers() {
  90. const pagerCount = this.pagerCount;
  91. const halfPagerCount = (pagerCount - 1) / 2;
  92. const currentPage = Number(this.currentPage);
  93. const pageCount = Number(this.pageCount);
  94. let showPrevMore = false;
  95. let showNextMore = false;
  96. if (pageCount > pagerCount) {
  97. if (currentPage > pagerCount - halfPagerCount) {
  98. showPrevMore = true;
  99. }
  100. if (currentPage < pageCount - halfPagerCount) {
  101. showNextMore = true;
  102. }
  103. }
  104. const array = [];
  105. if (showPrevMore && !showNextMore) {
  106. const startPage = pageCount - (pagerCount - 2);
  107. for (let i = startPage; i < pageCount; i++) {
  108. array.push(i);
  109. }
  110. } else if (!showPrevMore && showNextMore) {
  111. for (let i = 2; i < pagerCount; i++) {
  112. array.push(i);
  113. }
  114. } else if (showPrevMore && showNextMore) {
  115. const offset = Math.floor(pagerCount / 2) - 1;
  116. for (let i = currentPage - offset ; i <= currentPage + offset; i++) {
  117. array.push(i);
  118. }
  119. } else {
  120. for (let i = 2; i < pageCount; i++) {
  121. array.push(i);
  122. }
  123. }
  124. this.showPrevMore = showPrevMore;
  125. this.showNextMore = showNextMore;
  126. return array;
  127. }
  128. },
  129. data() {
  130. return {
  131. current: null,
  132. showPrevMore: false,
  133. showNextMore: false,
  134. quicknextIconClass: 'el-icon-more',
  135. quickprevIconClass: 'el-icon-more'
  136. };
  137. }
  138. };
  139. </script>