uni-pagination.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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="arrowleft" />
  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="arrowright" />
  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 { t } = initVueI18n(messages)
  61. export default {
  62. name: 'UniPagination',
  63. emits: ['update:modelValue', 'input', 'change'],
  64. props: {
  65. value: {
  66. type: [Number, String],
  67. default: 1
  68. },
  69. modelValue: {
  70. type: [Number, String],
  71. default: 1
  72. },
  73. prevText: {
  74. type: String,
  75. },
  76. nextText: {
  77. type: String,
  78. },
  79. current: {
  80. type: [Number, String],
  81. default: 1
  82. },
  83. total: {
  84. // 数据总量
  85. type: [Number, String],
  86. default: 0
  87. },
  88. pageSize: {
  89. // 每页数据量
  90. type: [Number, String],
  91. default: 10
  92. },
  93. showIcon: {
  94. // 是否以 icon 形式展示按钮
  95. type: [Boolean, String],
  96. default: false
  97. },
  98. pagerCount: {
  99. type: Number,
  100. default: 7
  101. }
  102. },
  103. data() {
  104. return {
  105. currentIndex: 1,
  106. paperData: []
  107. }
  108. },
  109. computed: {
  110. prevPageText() {
  111. return this.prevText || t('uni-pagination.prevText')
  112. },
  113. nextPageText() {
  114. return this.nextText || t('uni-pagination.nextText')
  115. },
  116. maxPage() {
  117. let maxPage = 1
  118. let total = Number(this.total)
  119. let pageSize = Number(this.pageSize)
  120. if (total && pageSize) {
  121. maxPage = Math.ceil(total / pageSize)
  122. }
  123. return maxPage
  124. },
  125. paper() {
  126. const num = this.currentIndex
  127. // TODO 最大页数
  128. const pagerCount = this.pagerCount
  129. // const total = 181
  130. const total = this.total
  131. const pageSize = this.pageSize
  132. let totalArr = []
  133. let showPagerArr = []
  134. let pagerNum = Math.ceil(total / pageSize)
  135. for (let i = 0; i < pagerNum; i++) {
  136. totalArr.push(i + 1)
  137. }
  138. showPagerArr.push(1)
  139. const totalNum = totalArr[totalArr.length - (pagerCount + 1) / 2]
  140. totalArr.forEach((item, index) => {
  141. if ((pagerCount + 1) / 2 >= num) {
  142. if (item < pagerCount + 1 && item > 1) {
  143. showPagerArr.push(item)
  144. }
  145. } else if (num + 2 <= totalNum) {
  146. if (item > num - (pagerCount + 1) / 2 && item < num + (pagerCount + 1) / 2) {
  147. showPagerArr.push(item)
  148. }
  149. } else {
  150. if ((item > num - (pagerCount + 1) / 2 || pagerNum - pagerCount < item) && item < totalArr[
  151. totalArr.length - 1]) {
  152. showPagerArr.push(item)
  153. }
  154. }
  155. })
  156. if (pagerNum > pagerCount) {
  157. if ((pagerCount + 1) / 2 >= num) {
  158. showPagerArr[showPagerArr.length - 1] = '...'
  159. } else if (num + 2 <= totalNum) {
  160. showPagerArr[1] = '...'
  161. showPagerArr[showPagerArr.length - 1] = '...'
  162. } else {
  163. showPagerArr[1] = '...'
  164. }
  165. showPagerArr.push(totalArr[totalArr.length - 1])
  166. } else {
  167. if ((pagerCount + 1) / 2 >= num) {} else if (num + 2 <= totalNum) {} else {
  168. showPagerArr.shift()
  169. showPagerArr.push(totalArr[totalArr.length - 1])
  170. }
  171. }
  172. return showPagerArr
  173. }
  174. },
  175. watch: {
  176. current(val) {
  177. this.currentIndex = val
  178. },
  179. value(val) {
  180. if (val < 1) {
  181. this.currentIndex = 1
  182. } else {
  183. this.currentIndex = val
  184. }
  185. },
  186. modelValue(val) {
  187. if (val < 1) {
  188. this.currentIndex = 1
  189. } else {
  190. this.currentIndex = val
  191. }
  192. }
  193. },
  194. created() {
  195. this.currentIndex = +this.value
  196. },
  197. methods: {
  198. // 选择标签
  199. selectPage(e, index) {
  200. if (parseInt(e)) {
  201. this.currentIndex = e
  202. this.change('current')
  203. } else {
  204. let pagerNum = Math.ceil(this.total / this.pageSize)
  205. // let pagerNum = Math.ceil(181 / this.pageSize)
  206. // 上一页
  207. if (index <= 1) {
  208. if (this.currentIndex - 5 > 1) {
  209. this.currentIndex -= 5
  210. } else {
  211. this.currentIndex = 1
  212. }
  213. return
  214. }
  215. // 下一页
  216. if (index >= 6) {
  217. if (this.currentIndex + 5 > pagerNum) {
  218. this.currentIndex = pagerNum
  219. } else {
  220. this.currentIndex += 5
  221. }
  222. return
  223. }
  224. }
  225. },
  226. clickLeft() {
  227. if (Number(this.currentIndex) === 1) {
  228. return
  229. }
  230. this.currentIndex -= 1
  231. this.change('prev')
  232. },
  233. clickRight() {
  234. if (Number(this.currentIndex) >= this.maxPage) {
  235. return
  236. }
  237. this.currentIndex += 1
  238. this.change('next')
  239. },
  240. change(e) {
  241. this.$emit('input', this.currentIndex)
  242. this.$emit('update:modelValue', this.currentIndex)
  243. this.$emit('change', {
  244. type: e,
  245. current: this.currentIndex
  246. })
  247. }
  248. }
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. .uni-pagination {
  253. /* #ifndef APP-NVUE */
  254. display: flex;
  255. /* #endif */
  256. position: relative;
  257. overflow: hidden;
  258. flex-direction: row;
  259. justify-content: center;
  260. align-items: center;
  261. }
  262. .uni-pagination__total {
  263. font-size: 14px;
  264. color: #999;
  265. margin-right: 15px;
  266. }
  267. .uni-pagination__btn {
  268. /* #ifndef APP-NVUE */
  269. display: flex;
  270. cursor: pointer;
  271. /* #endif */
  272. padding: 0 8px;
  273. line-height: 30px;
  274. font-size: $uni-font-size-base;
  275. position: relative;
  276. background-color: #f4f4f5;
  277. flex-direction: row;
  278. justify-content: center;
  279. align-items: center;
  280. text-align: center;
  281. // border-width: 1px;
  282. // border-style: solid;
  283. // border-color: $uni-border-color;
  284. }
  285. .uni-pagination__child-btn {
  286. /* #ifndef APP-NVUE */
  287. display: flex;
  288. /* #endif */
  289. font-size: $uni-font-size-base;
  290. position: relative;
  291. flex-direction: row;
  292. justify-content: center;
  293. align-items: center;
  294. text-align: center;
  295. }
  296. .uni-pagination__num {
  297. /* #ifndef APP-NVUE */
  298. display: flex;
  299. /* #endif */
  300. flex: 1;
  301. flex-direction: row;
  302. justify-content: center;
  303. align-items: center;
  304. height: 30px;
  305. line-height: 30px;
  306. font-size: $uni-font-size-base;
  307. color: $uni-text-color;
  308. margin: 0 5px;
  309. }
  310. .uni-pagination__num-tag {
  311. /* #ifdef H5 */
  312. cursor: pointer;
  313. min-width: 30px;
  314. /* #endif */
  315. margin: 0 5px;
  316. height: 30px;
  317. text-align: center;
  318. line-height: 30px;
  319. // border: 1px red solid;
  320. color: #666;
  321. // border-width: 1px;
  322. // border-style: solid;
  323. // border-color: $uni-border-color;
  324. }
  325. .uni-pagination__num-current {
  326. /* #ifndef APP-NVUE */
  327. display: flex;
  328. /* #endif */
  329. flex-direction: row;
  330. }
  331. .uni-pagination__num-current-text {
  332. font-size: 15px;
  333. }
  334. .uni-pagination--enabled {
  335. color: #333333;
  336. opacity: 1;
  337. }
  338. .uni-pagination--disabled {
  339. opacity: 0.5;
  340. /* #ifdef H5 */
  341. cursor: default;
  342. /* #endif */
  343. }
  344. .uni-pagination--hover {
  345. color: rgba(0, 0, 0, 0.6);
  346. background-color: $uni-bg-color-hover;
  347. }
  348. .tag--active:hover {
  349. color: $uni-color-primary;
  350. }
  351. .page--active {
  352. color: #fff;
  353. background-color: $uni-color-primary;
  354. }
  355. .page--active:hover {
  356. color: #fff;
  357. }
  358. /* #ifndef APP-NVUE */
  359. .is-pc-hide {
  360. display: block;
  361. }
  362. .is-phone-hide {
  363. display: none;
  364. }
  365. @media screen and (min-width: 450px) {
  366. .is-pc-hide {
  367. display: none;
  368. }
  369. .is-phone-hide {
  370. display: block;
  371. }
  372. .uni-pagination__num-flex-none {
  373. flex: none;
  374. }
  375. }
  376. /* #endif */
  377. </style>