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