page-pagination.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <template>
  2. <view class="contact page-pagination" :class="size=='large'?'size-large':size=='small'?'size-small':''">
  3. <view class="page-con">
  4. <view class="page-scroll">
  5. <view class="page-scroll-child">
  6. <!-- 首页 -->
  7. <view class="pag-btn start" :style="{color}" :class="[nowPage==1?'btn-ban':'',btnText?'btn-text':'']" v-if="showAround && !numAround" @click="clickStart">
  8. <text v-if="btnText">首页</text>
  9. <text v-else class="icon icon-left1"></text>
  10. </view>
  11. <!-- 上页 -->
  12. <view class="pag-btn prev" :style="{color}" :class="[nowPage==1?'btn-ban':'',btnText?'btn-text':'']" @click="clickPrev">
  13. <text v-if="btnText">上页</text>
  14. <text v-else class="icon icon-left"></text>
  15. </view>
  16. <!-- 简单模式 -->
  17. <view v-if="mode=='simple'" class="page-num simple"><text :style="{color}" style="margin-right: 10rpx;">{{nowPage}}</text>/ {{pageNum}}</view>
  18. <!-- 复杂模式 -->
  19. <view class="page-num" v-else>
  20. <template v-if="pageNum <= showPageSize">
  21. <view v-for="page in pageNumArr" :key="page" class="pag-btn" :style="{color, backgroundColor:(page==nowPage?color:'')}" :class="page==nowPage?'active':''" @click="clickPage(page)">{{page}}</view>
  22. </template>
  23. <template v-else>
  24. <!-- 第一页页码 -->
  25. <view class="pag-btn" :style="{color, backgroundColor:(nowPage==1?color:'')}" v-if="numAround" :class="nowPage==1?'active':''" @click="clickPage(1)">1</view>
  26. <!-- 左侧省略号 -->
  27. <view class="pag-btn ellipsis-btn" :style="{color}" v-show="(forceEllipses && getFirstPage != 1) || (numAround && getFirstPage != 2)"><text class="icon icon-more"></text></view>
  28. <template v-for="(p, i) in showPageSize">
  29. <template v-if="i<showPageSize-2">
  30. <!-- 中间页码 -->
  31. <view :key="i" class="pag-btn"
  32. :style="{color, backgroundColor:((getFirstPage+i)==nowPage?color:'')}"
  33. :class="(getFirstPage+i)==nowPage?'active':''"
  34. @click="clickPage(getFirstPage+i)">
  35. {{getFirstPage+i}}
  36. </view>
  37. </template>
  38. <template v-else>
  39. <!-- 若显示省略号,则页码只显示 showPageSize-2 页 -->
  40. <view :key="i" v-show="!forceEllipses && !numAround" class="pag-btn"
  41. :style="{color, backgroundColor:((getFirstPage+i)==nowPage?color:'')}"
  42. :class="(getFirstPage+i)==nowPage?'active':''"
  43. @click="clickPage(getFirstPage+i)">
  44. {{getFirstPage+i}}
  45. </view>
  46. </template>
  47. </template>
  48. <!-- 右侧省略号 -->
  49. <view class="pag-btn ellipsis-btn" :style="{color}" v-show="(forceEllipses && getFirstPage<pageNum-(showPageSize-3)) || (numAround && getFirstPage < pageNum-(showPageSize-2))">
  50. <text class="icon icon-more"></text>
  51. </view>
  52. <!-- 最后一页页码 -->
  53. <view class="pag-btn" :style="{color, backgroundColor:(nowPage==pageNum?color:'')}" v-if="numAround" v-show="getFirstPage < pageNum-(showPageSize-3)" :class="nowPage==pageNum?'active':''" @click="clickPage(pageNum)">{{pageNum}}</view>
  54. </template>
  55. </view>
  56. <!-- 下页 -->
  57. <view class="pag-btn next" :style="{color}" :class="[pageNum<=nowPage?'btn-ban':'',btnText?'btn-text':'']" @click="clickNext">
  58. <text v-if="btnText">下页</text>
  59. <text v-else class="icon icon-right"></text>
  60. </view>
  61. <!-- 尾页 -->
  62. <view class="pag-btn end" :style="{color}" :class="[pageNum<=nowPage?'btn-ban':'',btnText?'btn-text':'']" v-if="showAround && !numAround" @click="clickEnd">
  63. <text v-if="btnText">尾页</text>
  64. <text v-else class="icon icon-right1"></text>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. <view class="page-info">
  70. <slot />
  71. <view class="page-info-con" v-if="showPageInfo">
  72. <view>共 {{total}} 条,</view>
  73. <view>每页 {{pageSize}} 条,</view>
  74. <view>当前页 {{nowPage}}/{{pageNum}}<text v-if="showGoPage">,</text></view>
  75. </view>
  76. <view class="page-go" v-if="showGoPage">前往第
  77. <template v-if="trigger=='blur'">
  78. <input class="page-input" type="text" v-model="inputPage" @blur="goPage" @input="onInput" />页
  79. </template>
  80. <template v-else>
  81. <input class="page-input" type="text" v-model="inputPage" @input="onInput" />页
  82. <text v-if="trigger=='click'" class="page-input-btn" @click="goPage">跳转</text>
  83. </template>
  84. </view>
  85. </view>
  86. </view>
  87. </template>
  88. <script>
  89. /**
  90. * @property {Number} total 总条数,默认0
  91. * @property {Number} pageSize 每页条数,默认10
  92. * @property {Number} currentPage 当前页,默认1
  93. * @property {Number} showPageSize 显示的页码个数,默认5
  94. * @property {String} mode=['multi'|'simple'] 显示模式,默认multi
  95. * @property {Boolean} forceEllipses=[true|false] 是否显示省略号,默认false
  96. * @property {Boolean} btnText=[true|false] 是否显示汉字,默认false
  97. * @property {Boolean} showAround=[true|false] 是否显示首页和尾页,默认false
  98. * @property {Boolean} numAround=[true|false] 是否用页码表示首尾页,默认false
  99. * @property {String} size=['large'|'normal'|'small'] 按钮大小,默认normal
  100. * @property {Boolean} showPageInfo=[true|false] 是否显示分页信息,如总页数等,默认false
  101. * @property {Boolean} showGoPage=[true|false] 是否显示input输入框,默认false
  102. * @property {String} trigger=['blur'|'click'] 跳转页码触发方式,默认blur
  103. */
  104. export default {
  105. name: 'PagePagination',
  106. props: {
  107. //总条数
  108. total: {
  109. type: Number,
  110. default: 0
  111. },
  112. //每页条数
  113. pageSize: {
  114. type: Number,
  115. default: 10
  116. },
  117. //当前页
  118. currentPage: {
  119. type: Number,
  120. default: 1
  121. },
  122. //显示的页码个数
  123. showPageSize: {
  124. type: Number,
  125. default: 5
  126. },
  127. //显示模式
  128. mode: {
  129. type: String,
  130. default: "multi"
  131. },
  132. //是否显示省略号
  133. forceEllipses: {
  134. type: Boolean,
  135. default: false
  136. },
  137. //是否显示汉字
  138. btnText: {
  139. type: Boolean,
  140. default: false
  141. },
  142. //是否显示首页和尾页
  143. showAround: {
  144. type: Boolean,
  145. default: false
  146. },
  147. //是否用页码表示首尾页
  148. numAround: {
  149. type: Boolean,
  150. default: false
  151. },
  152. //按钮大小
  153. size: {
  154. type: String,
  155. default: "normal"
  156. },
  157. //页码颜色
  158. color: {
  159. type: String,
  160. default: "#ff4f00"
  161. },
  162. //是否显示页面信息
  163. showPageInfo: {
  164. type: Boolean,
  165. default: false
  166. },
  167. //是否显示前往跳转页
  168. showGoPage: {
  169. type: Boolean,
  170. default: false
  171. },
  172. //跳转页码触发方式
  173. trigger: {
  174. type: String,
  175. default: "blur"
  176. }
  177. },
  178. data() {
  179. return {
  180. nowPage: this.currentPage, //当前页
  181. inputPage: this.currentPage //input输入框绑定值
  182. }
  183. },
  184. watch: {
  185. currentPage(val) {
  186. this.nowPage = val > this.pageNum ? this.pageNum : val;
  187. this.inputPage = val > this.pageNum ? this.pageNum : val;
  188. if (val == 1) {
  189. this.nowPage = 1;
  190. this.inputPage = 1;
  191. }
  192. }
  193. },
  194. computed: {
  195. //总页数
  196. pageNum() {
  197. return Math.ceil(this.total / this.pageSize)
  198. },
  199. pageNumArr() { //解决uni-app某些版本中,存在v-for中循环数字时从0开始
  200. var pageNumArr = [];
  201. for (let i = 0; i < this.pageNum; i++) {
  202. pageNumArr.push(i + 1);
  203. }
  204. return pageNumArr;
  205. },
  206. //计算显示的第一个页码
  207. getFirstPage() {
  208. let firstPage = 0;
  209. let a = Math.floor((this.showPageSize - 1) / 2);
  210. let b = Math.floor(this.showPageSize / 2) - 1;
  211. if (this.nowPage <= a) {
  212. if (!this.numAround) firstPage = 1;
  213. else firstPage = 2;
  214. } else if (this.nowPage >= this.pageNum - b) {
  215. if (this.forceEllipses || this.numAround) firstPage = this.pageNum - this.showPageSize + 3;
  216. else firstPage = this.pageNum - this.showPageSize + 1;
  217. } else {
  218. if (this.forceEllipses || this.numAround) firstPage = this.nowPage - a + 1;
  219. else firstPage = this.nowPage - a;
  220. }
  221. return firstPage;
  222. }
  223. },
  224. methods: {
  225. //选择页码
  226. clickPage(page) {
  227. if (this.nowPage != page) {
  228. this.nowPage = page;
  229. this.change(page, 'page');
  230. }
  231. },
  232. //点击上页
  233. clickPrev() {
  234. if (this.nowPage > 1) {
  235. let nowPage = this.nowPage - 1;
  236. this.nowPage = nowPage;
  237. this.change(nowPage, 'prev');
  238. }
  239. },
  240. //点击下页
  241. clickNext() {
  242. if (this.nowPage < this.pageNum) {
  243. let nowPage = this.nowPage + 1;
  244. this.nowPage = nowPage;
  245. this.change(nowPage, 'next');
  246. }
  247. },
  248. //点击首页
  249. clickStart() {
  250. if (this.nowPage != 1) {
  251. let nowPage = 1;
  252. this.nowPage = nowPage;
  253. this.change(nowPage, 'homePage');
  254. }
  255. },
  256. //点击尾页
  257. clickEnd() {
  258. if (this.nowPage != this.pageNum) {
  259. let nowPage = this.pageNum;
  260. this.nowPage = nowPage;
  261. this.change(nowPage, 'endPage');
  262. }
  263. },
  264. //页码改变时触发
  265. change(nowPage, type) {
  266. this.inputPage = nowPage;
  267. this.$emit('change', nowPage, type);
  268. },
  269. onInput(event) {
  270. let self = this;
  271. let val = parseInt(event.target.value.replace(/[^\d]/g, ''));
  272. setTimeout(function() {
  273. self.inputPage = val ? (val > self.pageNum ? self.pageNum : val) : '';
  274. }, 10)
  275. },
  276. goPage() {
  277. this.nowPage = parseInt(this.inputPage ? this.inputPage : '1');
  278. this.inputPage = parseInt(this.inputPage ? this.inputPage : '1');
  279. this.change(parseInt(this.inputPage), 'goPage');
  280. }
  281. }
  282. }
  283. </script>
  284. <style>
  285. @import 'icon/iconfont.css';
  286. .page-pagination {
  287. width: 100%;
  288. }
  289. .page-pagination .page-con {
  290. width: 100%;
  291. display: flex;
  292. justify-content: center;
  293. }
  294. .page-pagination .page-con .page-scroll {
  295. display: flex;
  296. overflow-x: auto;
  297. overflow-y: hidden;
  298. margin: 0 auto;
  299. }
  300. .page-pagination .page-con .page-scroll-child {
  301. display: flex;
  302. white-space: nowrap;
  303. height: 62rpx;
  304. line-height: 62rpx;
  305. }
  306. .page-pagination .page-con .page-scroll-child .pag-btn {
  307. background-color: #FFF;
  308. color: #1989FA;
  309. font-size: 24rpx;
  310. border: 1px solid #EBEEF5;
  311. border-left: none;
  312. padding: 0 22rpx;
  313. }
  314. .page-pagination .page-con .page-scroll-child .pag-btn.start,
  315. .page-pagination .page-con .page-scroll-child .pag-btn.prev,
  316. .page-pagination .page-con .page-scroll-child .pag-btn.next,
  317. .page-pagination .page-con .page-scroll-child .pag-btn.end {
  318. padding: 0 14rpx;
  319. }
  320. .page-pagination .page-con .page-scroll-child .ellipsis-btn {
  321. padding: 0 10rpx;
  322. color: #999999 !important;
  323. }
  324. .page-pagination .page-con .page-scroll-child .pag-btn:has(> .icon-more) {
  325. padding: 0 14rpx;
  326. }
  327. .page-pagination .page-con .page-scroll-child .pag-btn:first-child {
  328. border-left: 1px solid #EBEEF5;
  329. }
  330. .page-pagination .page-con .page-scroll-child .pag-btn.active {
  331. background-color: #FF4F00;
  332. color: #FFFFFF !important;
  333. border: 1px solid #ff4f00;
  334. }
  335. .page-pagination .page-con .page-scroll-child .pag-btn.btn-ban {
  336. color: #C0C4CC !important;
  337. cursor: not-allowed;
  338. }
  339. .page-pagination .page-con .page-scroll-child .page-num {
  340. display: flex;
  341. }
  342. .page-pagination .page-con .page-scroll-child .page-num.simple {
  343. padding: 0 80rpx;
  344. line-height: 68rpx;
  345. color: #303133;
  346. font-size: 32rpx;
  347. }
  348. .page-pagination .page-con .page-scroll-child .icon-right,
  349. .page-pagination .page-con .page-scroll-child .icon-left {
  350. font-size: 26rpx;
  351. }
  352. .page-pagination .page-info {
  353. display: flex;
  354. justify-content: center;
  355. line-height: 56rpx;
  356. margin-top: 10rpx;
  357. color: #606266;
  358. font-size: 28rpx;
  359. text-align: center;
  360. }
  361. .page-pagination .page-info .page-info-con {
  362. display: flex;
  363. }
  364. .page-pagination .page-info .page-go {
  365. position: relative;
  366. top: -2rpx;
  367. }
  368. .page-pagination .page-info .page-go .page-input {
  369. display: inline-block;
  370. width: 80rpx;
  371. height: 52rpx;
  372. line-height: 52rpx;
  373. border: 1px solid #dcdfe6;
  374. border-radius: 8rpx;
  375. background-color: #FFF;
  376. padding: 0px 10rpx;
  377. margin: 0 10rpx;
  378. font-size: 28rpx;
  379. overflow: inherit;
  380. box-sizing: border-box;
  381. vertical-align: middle;
  382. }
  383. .page-pagination .page-info .page-go .page-input-btn {
  384. height: 52rpx;
  385. line-height: 52rpx;
  386. padding: 4rpx 14rpx;
  387. border-width: 1px;
  388. border-style: solid;
  389. border-radius: 3px;
  390. border-color: #DCDFE6;
  391. color: #303133;
  392. margin-left: 10rpx;
  393. opacity: 0.8;
  394. background-color: #FFF;
  395. }
  396. // 大号按钮
  397. .page-pagination.size-large .page-con .page-scroll-child {
  398. height: 78rpx;
  399. line-height: 78rpx;
  400. }
  401. .page-pagination.size-large .page-con .page-scroll-child .pag-btn {
  402. padding: 0 30rpx;
  403. font-size: 30rpx;
  404. }
  405. .page-pagination.size-large .page-con .page-scroll-child .pag-btn.start,
  406. .page-pagination.size-large .page-con .page-scroll-child .pag-btn.prev,
  407. .page-pagination.size-large .page-con .page-scroll-child .pag-btn.next,
  408. .page-pagination.size-large .page-con .page-scroll-child .pag-btn.end,
  409. .page-pagination.size-large .page-con .page-scroll-child .ellipsis-btn {
  410. padding: 0 24rpx;
  411. }
  412. .page-pagination.size-large .page-con .page-scroll-child .pag-btn.btn-text {
  413. padding: 0 12rpx;
  414. }
  415. // 小型按钮
  416. .page-pagination.size-small .page-con .page-scroll-child {
  417. height: 52rpx;
  418. line-height: 52rpx;
  419. }
  420. .page-pagination.size-small .page-con .page-scroll-child .pag-btn {
  421. padding: 0 14rpx;
  422. }
  423. .page-pagination.size-small .page-con .page-scroll-child .pag-btn.start,
  424. .page-pagination.size-small .page-con .page-scroll-child .pag-btn.prev,
  425. .page-pagination.size-small .page-con .page-scroll-child .pag-btn.next,
  426. .page-pagination.size-small .page-con .page-scroll-child .pag-btn.end {
  427. padding: 0 10rpx;
  428. }
  429. .page-pagination.size-small .page-con .page-scroll-child .ellipsis-btn {
  430. padding: 0 10rpx;
  431. }
  432. //解决nvue页面下不能正常显示问题
  433. [nvue-dir-column] .page-pagination uni-label,
  434. [nvue-dir-column] .page-pagination uni-swiper-item,
  435. [nvue-dir-column] .page-pagination uni-view {
  436. flex-direction: initial;
  437. }
  438. </style>