page-pagination.vue 14 KB

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