uni-pagination.vue 9.2 KB

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