uni-pagination.vue 8.9 KB

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