uni-pagination.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view class="uni-pagination">
  3. <view class="uni-pagination__btns">
  4. <view class="uni-pagination__btn" :class="currentIndex === 1 ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
  5. :hover-class="currentIndex === 1 ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70"
  6. @click="clickLeft">
  7. <template v-if="showIcon===true || showIcon === 'true'">
  8. <uni-icons color="#000" size="20" type="arrowleft" />
  9. </template>
  10. <template v-else><text class="uni-pagination__child-btn">{{ prevText }}</text></template>
  11. </view>
  12. <view class="uni-pagination__btn" :class="currentIndex === maxPage ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
  13. :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70"
  14. @click="clickRight">
  15. <template v-if="showIcon===true || showIcon === 'true'">
  16. <uni-icons color="#000" size="20" type="arrowright" />
  17. </template>
  18. <template v-else><text class="uni-pagination__child-btn">{{ nextText }}</text></template>
  19. </view>
  20. </view>
  21. <view class="uni-pagination__num">
  22. <view class="uni-pagination__num-current">
  23. <text class="uni-pagination__num-current-text" style="color:#007aff">{{ currentIndex }}</text><text class="uni-pagination__num-current-text">/{{ maxPage || 0 }}</text>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import uniIcons from '../uni-icons1/uni-icons.vue'
  30. export default {
  31. name: 'UniPagination',
  32. components: {
  33. uniIcons
  34. },
  35. props: {
  36. prevText: {
  37. type: String,
  38. default: '上一页'
  39. },
  40. nextText: {
  41. type: String,
  42. default: '下一页'
  43. },
  44. current: {
  45. type: [Number, String],
  46. default: 1
  47. },
  48. total: { // 数据总量
  49. type: [Number, String],
  50. default: 0
  51. },
  52. pageSize: { // 每页数据量
  53. type: [Number, String],
  54. default: 10
  55. },
  56. showIcon: { // 是否以 icon 形式展示按钮
  57. type: [Boolean, String],
  58. default: false
  59. }
  60. },
  61. data() {
  62. return {
  63. currentIndex: 1
  64. }
  65. },
  66. computed: {
  67. maxPage() {
  68. let maxPage = 1
  69. let total = Number(this.total)
  70. let pageSize = Number(this.pageSize)
  71. if (total && pageSize) {
  72. maxPage = Math.ceil(total / pageSize)
  73. }
  74. return maxPage
  75. }
  76. },
  77. watch: {
  78. current(val) {
  79. this.currentIndex = +val
  80. }
  81. },
  82. created() {
  83. this.currentIndex = +this.current
  84. },
  85. methods: {
  86. clickLeft() {
  87. if (Number(this.currentIndex) === 1) {
  88. return
  89. }
  90. this.currentIndex -= 1
  91. this.change('prev')
  92. },
  93. clickRight() {
  94. if (Number(this.currentIndex) === this.maxPage) {
  95. return
  96. }
  97. this.currentIndex += 1
  98. this.change('next')
  99. },
  100. change(e) {
  101. this.$emit('change', {
  102. type: e,
  103. current: this.currentIndex
  104. })
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .uni-pagination {
  111. /* #ifndef APP-NVUE */
  112. display: flex;
  113. /* #endif */
  114. /* #ifdef APP-NVUE */
  115. padding: 0 20px;
  116. /* #endif */
  117. width: 350px;
  118. position: relative;
  119. overflow: hidden;
  120. flex-direction: row;
  121. justify-content: center;
  122. align-items: center;
  123. }
  124. .uni-pagination__btns {
  125. /* #ifndef APP-NVUE */
  126. display: flex;
  127. /* #endif */
  128. flex: 1;
  129. justify-content: space-between;
  130. align-items: center;
  131. flex-direction: row;
  132. }
  133. .uni-pagination__btn {
  134. /* #ifndef APP-NVUE */
  135. display: flex;
  136. /* #endif */
  137. width: 80px;
  138. height: 40px;
  139. line-height: 40px;
  140. font-size: $uni-font-size-base;
  141. position: relative;
  142. background-color: $uni-bg-color-grey;
  143. flex-direction: row;
  144. justify-content: center;
  145. align-items: center;
  146. text-align: center;
  147. border-width: 1px;
  148. border-style: solid;
  149. border-color: $uni-border-color;
  150. border-radius: 8px;
  151. }
  152. .uni-pagination__child-btn {
  153. /* #ifndef APP-NVUE */
  154. display: flex;
  155. /* #endif */
  156. font-size: $uni-font-size-base;
  157. position: relative;
  158. flex-direction: row;
  159. justify-content: center;
  160. align-items: center;
  161. text-align: center;
  162. }
  163. .uni-pagination__num {
  164. /* #ifndef APP-NVUE */
  165. display: flex;
  166. /* #endif */
  167. position: absolute;
  168. left: 150px;
  169. top: 0;
  170. flex-direction: row;
  171. justify-content: center;
  172. align-items: center;
  173. width: 50px;
  174. height: 30px;
  175. line-height: 30px;
  176. font-size: $uni-font-size-base;
  177. color: $uni-text-color;
  178. }
  179. .uni-pagination__num-current {
  180. /* #ifndef APP-NVUE */
  181. display: flex;
  182. /* #endif */
  183. flex-direction: row;
  184. }
  185. .uni-pagination__num-current-text {
  186. font-size: 15px;
  187. }
  188. .uni-pagination--enabled {
  189. color: #333333;
  190. opacity: 1;
  191. }
  192. .uni-pagination--disabled {
  193. opacity: 0.3;
  194. }
  195. .uni-pagination--hover {
  196. color: rgba(0, 0, 0, .6);
  197. background-color: $uni-bg-color-hover;
  198. }
  199. </style>