-
Notifications
You must be signed in to change notification settings - Fork 9
/
u-pagination.vue
189 lines (172 loc) · 4.67 KB
/
u-pagination.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<template>
<ul class="u-pagination">
<li class="item" role="prev" :disabled="currentPage <= 1" @click="select(currentPage - 1)">
<
</li>
<template v-for="page in pages">
<li
v-if="page"
:key="page"
class="item"
:selected="currentPage === page"
@click="select(page)"
>
{{ page }}
</li>
<li v-else :key="page" class="item" role="blank">...</li>
</template>
<li
class="item"
role="next"
:disabled="currentPage >= totalPage"
@click="select(currentPage + 1)"
>
>
</li>
</ul>
</template>
<script>
export default {
name: 'u-pagination',
props: {
// 数据总条数
total: {
type: Number,
default: 1,
validator: value => Number.isInteger(value) && value >= 0
},
// 每页数据。总页数 = 数据总条数/每页数据
size: {
type: Number,
default: 20,
validator: value => Number.isInteger(value) && value >= 0
},
// 当前页数
page: {
type: Number,
default: 1,
validator: value => Number.isInteger(value) && value > 0
},
// 两侧数量
side: {
type: Number,
default: 2,
validator: value => Number.isInteger(value) && value > 0
},
// 中间数量
around: {
type: Number,
default: 3,
validator: value => Number.isInteger(value) && value > 0 && value % 2 === 1
}
},
data() {
return {
currentPage: this.page
}
},
watch: {
page(page) {
this.currentPage = page
}
},
computed: {
totalPage() {
return Math.ceil(this.total / this.size)
},
pages() {
const pages = []
const part = this.around >> 1
let start = this.currentPage - part
let end = start + this.around - 1
if (start < 1) {
end += 1 - start
start = 1
} else if (end > this.totalPage) {
start -= end - this.totalPage
end = this.totalPage
}
start = Math.max(1, Math.min(start, this.totalPage - this.side + 1))
end = Math.min(this.totalPage, Math.max(end, this.side))
let page = 1
while (page <= this.totalPage) {
if (
page <= this.side ||
(page >= start && page <= end) ||
page > this.totalPage - this.side
)
pages.push(page)
else {
pages.push(undefined)
if (page < start) page = start - 1
if (page > end) page = this.totalPage - this.side
}
page++
}
return pages
}
},
methods: {
select(page) {
if (page < 1 || page > this.totalPage || page === this.currentPage) return
this.currentPage = page
this.$emit('update:page', page) // allow sync api
this.$emit('input', page) // allow v-model api
}
}
}
</script>
<style lang="scss" scoped>
$item_size: 32px;
.u-pagination {
user-select: none;
cursor: default;
padding: 0;
text-align: right;
.total {
display: inline-block;
margin-right: 8px;
color: #999;
}
.item {
display: inline-block;
text-decoration: none;
min-width: $item_size;
height: $item_size;
line-height: $item_size;
text-align: center;
margin-right: 8px;
background: white;
border: 1px solid #e1e3e6;
border-radius: 2px;
cursor: pointer;
&:hover {
background: #ebf2ff;
border-color: $primary-color;
&[disabled] {
background: white;
border: 1px solid #e1e3e6;
}
}
&[selected] {
background: $primary-color;
color: white;
}
&[disabled] {
cursor: not-allowed;
color: $disabled-color;
}
.item[role='prev']::before {
content: '‹';
}
.item[role='next']::before {
content: '›';
}
&[role='blank'] {
background: none;
color: gray;
cursor: initial;
}
}
}
</style>