uni-pagination.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view class="uni-pagination">
  3. <view class="uni-pagination__btn" :class="currentIndex === 1 ? 'uni-pagination--disabled' : 'uni-pagination--enabled'" :hover-class="currentIndex === 1 ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70" @click="clickLeft">
  4. <template v-if="showIcon===true || showIcon === 'true'">
  5. <uni-icons color="#000" size="20" type="arrowleft" />
  6. </template>
  7. <template v-else><text class="uni-pagination__child-btn">{{ prevText }}</text></template>
  8. </view>
  9. <view class="uni-pagination__num">
  10. <view class="uni-pagination__num-current">
  11. <text class="uni-pagination__num-current-text" style="color:#007aff">{{ currentIndex }}</text><text class="uni-pagination__num-current-text">/{{ maxPage || 0 }}</text>
  12. </view>
  13. </view>
  14. <view class="uni-pagination__btn" :class="currentIndex === maxPage ? 'uni-pagination--disabled' : 'uni-pagination--enabled'" :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70" @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. </template>
  22. <script>
  23. import uniIcons from '../uni-icons/uni-icons.vue'
  24. /**
  25. * Pagination 分页器
  26. * @description 分页器组件,用于展示页码、请求数据等
  27. * @tutorial https://ext.dcloud.net.cn/plugin?id=32
  28. * @property {String} prevText 左侧按钮文字
  29. * @property {String} nextText 右侧按钮文字
  30. * @property {Number} current 当前页
  31. * @property {Number} total 数据总量
  32. * @property {Number} pageSize 每页数据量
  33. * @property {Number} showIcon = [true|false] 是否以 icon 形式展示按钮
  34. * @event {Function} change 点击页码按钮时触发 ,e={type,current} current为当前页,type值为:next/prev,表示点击的是上一页还是下一个
  35. */
  36. export default {
  37. name: 'UniPagination',
  38. components: {
  39. uniIcons
  40. },
  41. props: {
  42. prevText: {
  43. type: String,
  44. default: '上一页'
  45. },
  46. nextText: {
  47. type: String,
  48. default: '下一页'
  49. },
  50. current: {
  51. type: [Number, String],
  52. default: 1
  53. },
  54. total: { // 数据总量
  55. type: [Number, String],
  56. default: 0
  57. },
  58. pageSize: { // 每页数据量
  59. type: [Number, String],
  60. default: 10
  61. },
  62. showIcon: { // 是否以 icon 形式展示按钮
  63. type: [Boolean, String],
  64. default: false
  65. }
  66. },
  67. data() {
  68. return {
  69. currentIndex: 1
  70. }
  71. },
  72. computed: {
  73. maxPage() {
  74. let maxPage = 1
  75. let total = Number(this.total)
  76. let pageSize = Number(this.pageSize)
  77. if (total && pageSize) {
  78. maxPage = Math.ceil(total / pageSize)
  79. }
  80. return maxPage
  81. }
  82. },
  83. watch: {
  84. current(val) {
  85. this.currentIndex = +val
  86. }
  87. },
  88. created() {
  89. this.currentIndex = +this.current
  90. },
  91. methods: {
  92. clickLeft() {
  93. if (Number(this.currentIndex) === 1) {
  94. return
  95. }
  96. this.currentIndex -= 1
  97. this.change('prev')
  98. },
  99. clickRight() {
  100. if (Number(this.currentIndex) === this.maxPage) {
  101. return
  102. }
  103. this.currentIndex += 1
  104. this.change('next')
  105. },
  106. change(e) {
  107. this.$emit('change', {
  108. type: e,
  109. current: this.currentIndex
  110. })
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped>
  116. .uni-pagination {
  117. /* #ifndef APP-NVUE */
  118. display: flex;
  119. /* #endif */
  120. position: relative;
  121. overflow: hidden;
  122. flex-direction: row;
  123. justify-content: center;
  124. align-items: center;
  125. }
  126. .uni-pagination__btn {
  127. /* #ifndef APP-NVUE */
  128. display: flex;
  129. /* #endif */
  130. width: 60px;
  131. height: 30px;
  132. line-height: 30px;
  133. font-size: 14px;
  134. position: relative;
  135. background-color: #f8f8f8;
  136. flex-direction: row;
  137. justify-content: center;
  138. align-items: center;
  139. text-align: center;
  140. border-width: 1px;
  141. border-style: solid;
  142. border-color: #e5e5e5;
  143. }
  144. .uni-pagination__child-btn {
  145. /* #ifndef APP-NVUE */
  146. display: flex;
  147. /* #endif */
  148. font-size: 14px;
  149. position: relative;
  150. flex-direction: row;
  151. justify-content: center;
  152. align-items: center;
  153. text-align: center;
  154. }
  155. .uni-pagination__num {
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. flex: 1;
  160. flex-direction: row;
  161. justify-content: center;
  162. align-items: center;
  163. height: 30px;
  164. line-height: 30px;
  165. font-size: 14px;
  166. color: #333;
  167. }
  168. .uni-pagination__num-current {
  169. /* #ifndef APP-NVUE */
  170. display: flex;
  171. /* #endif */
  172. flex-direction: row;
  173. }
  174. .uni-pagination__num-current-text {
  175. font-size: 15px;
  176. }
  177. .uni-pagination--enabled {
  178. color: #333333;
  179. opacity: 1;
  180. }
  181. .uni-pagination--disabled {
  182. opacity: 0.3;
  183. }
  184. .uni-pagination--hover {
  185. color: rgba(0, 0, 0, .6);
  186. background-color: #f1f1f1;
  187. }
  188. </style>