-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue页码.html
210 lines (195 loc) · 7.3 KB
/
vue页码.html
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.pager-wrapper {
margin-top: 15px;
text-align: center;
}
.pager-pages {
display: inline-block;
height: 32px;
font-size: 0;
}
.pager-wrapper a,
.pager-wrapper strong {
display: inline-block;
min-width: 32px;
height: 32px;
padding: 0 10px;
margin: 0 2px;
font-size: 14px;
line-height: 32px;
text-align: center;
color: #222;
}
.pager-wrapper a {
border: 1px solid #ddd;
border-radius: 2px;
background-color: #fff;
transition: all .2s;
}
.pager-wrapper a:hover {
color: rgb(33, 150, 243);
border-color: rgb(33, 150, 243);
}
.pager-wrapper .active {
background-color: rgb(33, 150, 243);
color: #fff;
border-color: rgb(33, 150, 243);
}
.pager-wrapper .active:hover {
color: #fff;
}
.pager-jump {
display: inline-block;
height: 32px;
margin-left: 20px;
}
.pager-jump span {
line-height: 32px;
}
.pager-jump em {
margin: 0 5px;
font-style: normal;
}
.pager-jump .jump-input {
width: 60px;
height: 32px;
padding: 5px;
outline: none;
border: 1px solid #ddd;
font-size: 14px;
vertical-align: top;
}
.pager-jump .jump-input:focus {
border-color: rgb(33, 150, 243);
}
</style>
</head>
<body>
<div id="app">
<div class="pager-wrapper" v-if="totalPage > 0">
<div class="pager-pages">
<a v-show="currentPage > 1 && showPrev" @click="go(currentPage - 1)">上一页</a>
<a :class="{active: currentPage == 1 ? true : false}" @click="go(1)">1</a>
<strong v-show="pages[0] > 2">...</strong>
<a v-for="page in pages" :class="{active: currentPage == page ? true : false}" @click="go(page)">{{page}}</a>
<strong v-show="pages[pages.length-1] < totalPage - 1">...</strong>
<a v-if="totalPage > 1" :class="{active: currentPage == totalPage ? true : false}" @click="go(totalPage)">{{totalPage}}</a>
<a v-show="currentPage < totalPage && showNext" @click="go(currentPage + 1)">下一页</a>
</div>
<div v-if="showJump" v-show="totalPage > 1" class="pager-jump">
<span>共
<em class="jump-total">{{totalPage}}</em>页 ,跳至</span>
<input type="number" min="1" :max="totalPage" v-model="jumpPage" class="jump-input">
<span>页</span>
<a @click="go(jumpPage)">确定</a>
</div>
</div>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.15/vue.min.js"></script>
<script>
/*
* component pager 翻页页码组件
*/
var vm = new Vue({
el:"#app",
data:{
currentPage: 1,
jumpPage: 0,
totalPage: 10, // 总页数
showItems: 3, // 显示出来的页数,如: 1 ... 34[5]67 ... 10
showPrev: true, // 是否显示“上一页”
showNext: true, // 是否显示“下一页”
showJump: true, // 是否显示“跳转”
initPage: 3,
mode: 'event', // 'event' | 'query' | 'params',
routeName: ""
},
created() {
this.currentPage = this.initPage
if (this.mode == 'params' && !this.routeName) {
throw 'need a route name when choose params mode in pager component'
}
},
computed: {
pages() {
// 根据起始页码和结束页码得到页码数组
let getPages = (start, end) => {
if (start <= 1 || start > end || start >= this.totalPage) {
start = 2
}
if (end >= this.totalPage || end < start || end <= 1) {
end = this.totalPage - 1
}
let arr = []
for (let i = start; i <= end; i++) {
arr.push(i)
}
return arr
}
let counts = this.showItems
if (this.totalPage < counts + 2) {
return getPages(2, this.totalPage)
} else {
if (this.currentPage <= Math.ceil(counts / 2)) {
return getPages(2, counts)
} else if (this.currentPage >= this.totalPage - Math.floor(counts / 2)) {
return getPages(this.totalPage + 1 - counts, this.totalPage - 1)
} else {
let half = Math.ceil(counts / 2) - 1
let end = this.currentPage + half
if (counts % 2 === 0) {
end++
}
return getPages(this.currentPage - half, end)
}
}
}
},
methods: {
go(page) {
if (page < 1) {
page = 1
}
if (page > this.totalPage) {
page = this.totalPage
}
if (page === this.currentPage) {
return
}
this.currentPage = parseInt(page, 10)
if (this.mode == 'query') {
let query = this.$route.query
query.page = this.currentPage
this.$router.go({ query: query })
} else if (this.mode == 'params') {
let params = this.$route.params
params.page = this.currentPage
this.$router.go({ name: this.routeName, params: params })
} else {
this.$emit('go-page', {
page: this.currentPage
})
}
}
},
watch: {
currentPage(newVal) {
this.jumpPage = newVal
},
initPage(newVal) {
if (this.currentPage !== newVal) {
this.currentPage = newVal
}
}
}
})
</script>
</body>
</html>