uni-pagination-built-in.vue 12 KB

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