uni-pagination.vue 11 KB

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