paging.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div class="layer">
  3. <span class="total">共{{total}}条</span>
  4. <div class="number">
  5. <span @click="current = current - 1"><i class="iconfont icon_back"></i></span>
  6. <template v-if="currMin > min">
  7. <span @click="current = min">{{min}}</span>
  8. <span v-if="currMin - 1 > min">…</span>
  9. </template>
  10. <span
  11. v-for="index in numbers"
  12. :key="index"
  13. :class="{active: index === current}"
  14. @click="current = index">
  15. {{index}}
  16. </span>
  17. <template v-if="currMax < max">
  18. <span v-if="currMax + 1 < max">…</span>
  19. <span @click="current = max">{{max}}</span>
  20. </template>
  21. <span @click="current = current + 1"><i class="iconfont icon_forward"></i></span>
  22. </div>
  23. <div class="goto">
  24. <span>前往</span>
  25. <input
  26. type="text"
  27. :value="current"
  28. @blur="ev => ev.target.value = current"
  29. @keyup.enter="ev => current = Number(ev.target.value)"
  30. >
  31. <span>页</span>
  32. </div>
  33. </div>
  34. </template>
  35. <script>
  36. export default {
  37. props: ['paging'],
  38. data() {
  39. return {
  40. ...this.paging
  41. }
  42. },
  43. computed: {
  44. min() {
  45. return 1
  46. },
  47. max() {
  48. return Math.ceil(this.total / this.pageSize)
  49. },
  50. routineMin() {
  51. return this.current - Math.ceil(this.showSize / 2)
  52. },
  53. routineMax() {
  54. return this.current + Math.floor(this.showSize / 2)
  55. },
  56. currMin() {
  57. let c = this.max - this.routineMax
  58. if (this.routineMin <= this.min) {
  59. return this.min
  60. } else if (c >= 0) {
  61. return this.routineMin
  62. } else if (this.routineMin + c <= this.min) {
  63. return this.min
  64. } else {
  65. return this.routineMin + c
  66. }
  67. },
  68. currMax() {
  69. let c = this.min - this.routineMin
  70. if (this.routineMax >= this.max) {
  71. return this.max
  72. } else if (c <= 0) {
  73. return this.routineMax
  74. } else if (this.routineMax + c >= this.max) {
  75. return this.max
  76. } else {
  77. return this.routineMax + c
  78. }
  79. },
  80. numbers() {
  81. let total = this.currMax - this.currMin
  82. let numbers = []
  83. if (total === 0) {
  84. numbers.push(1)
  85. } else {
  86. for (let i = 0; i <= total; i++) {
  87. numbers.push(this.currMin + i)
  88. }
  89. }
  90. return numbers
  91. }
  92. },
  93. watch: {
  94. current(val, old) {
  95. if (isNaN(this.current)) {
  96. return this.current = old
  97. } else if (this.current < this.min) {
  98. return this.current = this.min
  99. } else if (this.current > this.max) {
  100. return this.current = this.max
  101. }
  102. this.$emit('changeCurrent', this.current)
  103. },
  104. paging() {
  105. Object.keys(this.paging).forEach(k => this[k] = this.paging[k])
  106. },
  107. 'paging.total':function () {
  108. Object.keys(this.paging).forEach(k => this[k] = this.paging[k])
  109. }
  110. }
  111. }
  112. </script>
  113. <style lang="less" scoped>
  114. .layer {
  115. span {
  116. font-size: 12px;
  117. color:#202020;
  118. display: inline-block;
  119. vertical-align: middle;
  120. margin: 0 10px;
  121. &.active {
  122. color: #0076F6;
  123. }
  124. }
  125. .number span {
  126. margin: 0 5px;
  127. cursor: pointer;
  128. &:first-of-type{
  129. margin-left: 0;
  130. }
  131. }
  132. div {
  133. display: inline-block;
  134. }
  135. div input {
  136. background: none;
  137. border:1px solid #EBEBEB;
  138. width:46px;
  139. height:28px;
  140. border-radius:2px;
  141. vertical-align: middle;
  142. text-align: center;
  143. color: #202020;
  144. }
  145. .goto{
  146. span{
  147. color: #909090;
  148. }
  149. }
  150. }
  151. </style>