uni-pagination.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <view class="uni-pagination">
  3. <!-- #ifndef APP-NVUE -->
  4. <view class="uni-pagination__total is-phone-hide">共 {{ total }} 条</view>
  5. <!-- #endif -->
  6. <view
  7. class="uni-pagination__btn"
  8. :class="currentIndex === 1 ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
  9. :hover-class="currentIndex === 1 ? '' : 'uni-pagination--hover'"
  10. :hover-start-time="20"
  11. :hover-stay-time="70"
  12. @click="clickLeft"
  13. >
  14. <template v-if="showIcon === true || showIcon === 'true'">
  15. <uni-icons color="#666" size="16" type="arrowleft" />
  16. </template>
  17. <template v-else>
  18. <text class="uni-pagination__child-btn">{{ prevText }}</text>
  19. </template>
  20. </view>
  21. <view class="uni-pagination__num uni-pagination__num-flex-none">
  22. <view class="uni-pagination__num-current">
  23. <text class="uni-pagination__num-current-text is-pc-hide" style="color:#409EFF">{{ currentIndex }}</text>
  24. <text class="uni-pagination__num-current-text is-pc-hide">/{{ maxPage || 0 }}</text>
  25. <!-- #ifndef APP-NVUE -->
  26. <view
  27. v-for="(item, index) in paper"
  28. :key="index"
  29. :class="{ 'page--active': item === currentIndex }"
  30. class="uni-pagination__num-tag tag--active is-phone-hide"
  31. @click.top="selectPage(item, index)"
  32. >
  33. <text>{{ item }}</text>
  34. </view>
  35. <!-- #endif -->
  36. </view>
  37. </view>
  38. <view
  39. class="uni-pagination__btn"
  40. :class="currentIndex >= maxPage ? 'uni-pagination--disabled' : 'uni-pagination--enabled'"
  41. :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'"
  42. :hover-start-time="20"
  43. :hover-stay-time="70"
  44. @click="clickRight"
  45. >
  46. <template v-if="showIcon === true || showIcon === 'true'">
  47. <uni-icons color="#666" size="16" type="arrowright" />
  48. </template>
  49. <template v-else>
  50. <text class="uni-pagination__child-btn">{{ nextText }}</text>
  51. </template>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. /**
  57. * Pagination 分页器
  58. * @description 分页器组件,用于展示页码、请求数据等
  59. * @tutorial https://ext.dcloud.net.cn/plugin?id=32
  60. * @property {String} prevText 左侧按钮文字
  61. * @property {String} nextText 右侧按钮文字
  62. * @property {Number} current 当前页
  63. * @property {Number} total 数据总量
  64. * @property {Number} pageSize 每页数据量
  65. * @property {Number} showIcon = [true|false] 是否以 icon 形式展示按钮
  66. * @event {Function} change 点击页码按钮时触发 ,e={type,current} current为当前页,type值为:next/prev,表示点击的是上一页还是下一个
  67. */
  68. export default {
  69. name: 'UniPagination',
  70. props: {
  71. value: {
  72. type: [Number, String],
  73. default: 1
  74. },
  75. prevText: {
  76. type: String,
  77. default: '上一页'
  78. },
  79. nextText: {
  80. type: String,
  81. default: '下一页'
  82. },
  83. current: {
  84. type: [Number, String],
  85. default: 1
  86. },
  87. total: {
  88. // 数据总量
  89. type: [Number, String],
  90. default: 0
  91. },
  92. pageSize: {
  93. // 每页数据量
  94. type: [Number, String],
  95. default: 10
  96. },
  97. showIcon: {
  98. // 是否以 icon 形式展示按钮
  99. type: [Boolean, String],
  100. default: false
  101. },
  102. pagerCount: {
  103. type: Number,
  104. default: 7
  105. }
  106. },
  107. data() {
  108. return {
  109. currentIndex: 1,
  110. paperData: []
  111. }
  112. },
  113. computed: {
  114. maxPage() {
  115. let maxPage = 1
  116. let total = Number(this.total)
  117. let pageSize = Number(this.pageSize)
  118. if (total && pageSize) {
  119. maxPage = Math.ceil(total / pageSize)
  120. }
  121. return maxPage
  122. },
  123. paper() {
  124. const num = this.currentIndex
  125. // TODO 最大页数
  126. const pagerCount = this.pagerCount
  127. // const total = 181
  128. const total = this.total
  129. const pageSize = this.pageSize
  130. let totalArr = []
  131. let showPagerArr = []
  132. let pagerNum = Math.ceil(total / pageSize)
  133. for (let i = 0; i < pagerNum; i++) {
  134. totalArr.push(i + 1)
  135. }
  136. showPagerArr.push(1)
  137. const totalNum = totalArr[totalArr.length - (pagerCount + 1) / 2]
  138. totalArr.forEach((item, index) => {
  139. if ((pagerCount + 1) / 2 >= num) {
  140. if (item < pagerCount + 1 && item > 1) {
  141. showPagerArr.push(item)
  142. }
  143. } else if (num + 2 <= totalNum) {
  144. if (item > num - (pagerCount + 1) / 2 && item < num + (pagerCount + 1) / 2) {
  145. showPagerArr.push(item)
  146. }
  147. } else {
  148. if ((item > num - (pagerCount + 1) / 2 || pagerNum - pagerCount < item) && item < totalArr[totalArr.length - 1]) {
  149. showPagerArr.push(item)
  150. }
  151. }
  152. })
  153. if (pagerNum > pagerCount) {
  154. if ((pagerCount + 1) / 2 >= num) {
  155. showPagerArr[showPagerArr.length - 1] = '...'
  156. } else if (num + 2 <= totalNum) {
  157. showPagerArr[1] = '...'
  158. showPagerArr[showPagerArr.length - 1] = '...'
  159. } else {
  160. showPagerArr[1] = '...'
  161. }
  162. showPagerArr.push(totalArr[totalArr.length - 1])
  163. } else {
  164. if ((pagerCount + 1) / 2 >= num) {
  165. } else if (num + 2 <= totalNum) {
  166. } else {
  167. showPagerArr.shift()
  168. showPagerArr.push(totalArr[totalArr.length - 1])
  169. }
  170. }
  171. return showPagerArr
  172. }
  173. },
  174. watch: {
  175. current(val) {
  176. this.currentIndex = val
  177. },
  178. value(val) {
  179. if (val < 1) {
  180. this.currentIndex = 1
  181. } else {
  182. this.currentIndex = val
  183. }
  184. }
  185. },
  186. created() {
  187. this.currentIndex = +this.value
  188. },
  189. methods: {
  190. // 选择标签
  191. selectPage(e, index) {
  192. if (parseInt(e)) {
  193. this.currentIndex = e
  194. this.change('current')
  195. } else {
  196. let pagerNum = Math.ceil(this.total / this.pageSize)
  197. // let pagerNum = Math.ceil(181 / this.pageSize)
  198. // 上一页
  199. if (index <= 1) {
  200. if (this.currentIndex - 5 > 1) {
  201. this.currentIndex -= 5
  202. } else {
  203. this.currentIndex = 1
  204. }
  205. return
  206. }
  207. // 下一页
  208. if (index >= 6) {
  209. if (this.currentIndex + 5 > pagerNum) {
  210. this.currentIndex = pagerNum
  211. } else {
  212. this.currentIndex += 5
  213. }
  214. return
  215. }
  216. }
  217. },
  218. clickLeft() {
  219. if (Number(this.currentIndex) === 1) {
  220. return
  221. }
  222. this.currentIndex -= 1
  223. this.change('prev')
  224. },
  225. clickRight() {
  226. if (Number(this.currentIndex) >= this.maxPage) {
  227. return
  228. }
  229. this.currentIndex += 1
  230. this.change('next')
  231. },
  232. change(e) {
  233. this.$emit('input', this.currentIndex)
  234. this.$emit('change', {
  235. type: e,
  236. current: this.currentIndex
  237. })
  238. }
  239. }
  240. }
  241. </script>
  242. <style lang="scss" scoped>
  243. .uni-pagination {
  244. /* #ifndef APP-NVUE */
  245. display: flex;
  246. /* #endif */
  247. position: relative;
  248. overflow: hidden;
  249. flex-direction: row;
  250. justify-content: center;
  251. align-items: center;
  252. }
  253. .uni-pagination__total {
  254. font-size: 14px;
  255. color: #999;
  256. margin-right: 15px;
  257. }
  258. .uni-pagination__btn {
  259. /* #ifndef APP-NVUE */
  260. display: flex;
  261. cursor: pointer;
  262. /* #endif */
  263. padding: 0 8px;
  264. line-height: 30px;
  265. font-size: $uni-font-size-base;
  266. position: relative;
  267. background-color: #f4f4f5;
  268. flex-direction: row;
  269. justify-content: center;
  270. align-items: center;
  271. text-align: center;
  272. // border-width: 1px;
  273. // border-style: solid;
  274. // border-color: $uni-border-color;
  275. }
  276. .uni-pagination__child-btn {
  277. /* #ifndef APP-NVUE */
  278. display: flex;
  279. /* #endif */
  280. font-size: $uni-font-size-base;
  281. position: relative;
  282. flex-direction: row;
  283. justify-content: center;
  284. align-items: center;
  285. text-align: center;
  286. }
  287. .uni-pagination__num {
  288. /* #ifndef APP-NVUE */
  289. display: flex;
  290. /* #endif */
  291. flex: 1;
  292. flex-direction: row;
  293. justify-content: center;
  294. align-items: center;
  295. height: 30px;
  296. line-height: 30px;
  297. font-size: $uni-font-size-base;
  298. color: $uni-text-color;
  299. margin: 0 5px;
  300. }
  301. .uni-pagination__num-tag {
  302. /* #ifdef H5 */
  303. cursor: pointer;
  304. min-width: 30px;
  305. /* #endif */
  306. margin: 0 5px;
  307. height: 30px;
  308. text-align: center;
  309. line-height: 30px;
  310. // border: 1px red solid;
  311. color: #666;
  312. // border-width: 1px;
  313. // border-style: solid;
  314. // border-color: $uni-border-color;
  315. }
  316. .uni-pagination__num-current {
  317. /* #ifndef APP-NVUE */
  318. display: flex;
  319. /* #endif */
  320. flex-direction: row;
  321. }
  322. .uni-pagination__num-current-text {
  323. font-size: 15px;
  324. }
  325. .uni-pagination--enabled {
  326. color: #333333;
  327. opacity: 1;
  328. }
  329. .uni-pagination--disabled {
  330. opacity: 0.5;
  331. /* #ifdef H5 */
  332. cursor: default;
  333. /* #endif */
  334. }
  335. .uni-pagination--hover {
  336. color: rgba(0, 0, 0, 0.6);
  337. background-color: $uni-bg-color-hover;
  338. }
  339. .tag--active:hover {
  340. color: $uni-color-primary;
  341. }
  342. .page--active {
  343. color: #fff;
  344. background-color: $uni-color-primary;
  345. }
  346. .page--active:hover {
  347. color: #fff;
  348. }
  349. /* #ifndef APP-NVUE */
  350. .is-pc-hide {
  351. display: block;
  352. }
  353. .is-phone-hide {
  354. display: none;
  355. }
  356. @media screen and (min-width: 450px) {
  357. .is-pc-hide {
  358. display: none;
  359. }
  360. .is-phone-hide {
  361. display: block;
  362. }
  363. .uni-pagination__num-flex-none {
  364. flex: none;
  365. }
  366. }
  367. /* #endif */
  368. </style>