uni-pagination.vue 11 KB

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