Vue分页
0 Views 前端 with
本文字数:760 字 | 阅读时长 ≈ 4 min

Vue分页

0 Views 前端 with
本文字数:760 字 | 阅读时长 ≈ 4 min

这篇文章主要介绍了Vue分页的实现

Demo

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>vue分页</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue-resource/1.5.0/vue-resource.js"></script>
<style>
.content ul {
width: 200px;
margin: 100px auto;
border: 1px solid #000;
}

ul,
li {
list-style: none;
margin: 0;
padding: 0;
}

.f_left {
float: left;
}

.clearfix:after {
content: '';
display: block;
clear: both;
height: 0;
visibility: hidden;
}

.page_bar {
margin: 100px auto;
text-align: center;
}

.page_bar a,
.page_bar span {
display: inline-block;
padding: 5px 12px;
border: 1px solid gray;
margin: 0;
text-decoration: none;
letter-spacing: 0;
word-spacing: 0;
border-radius: 4px;
}

.page_bar .page {
margin: 0 2px;
}

.page_bar .page.active {
background: gray;
color: #fff;
}

[v-cloak] {
display: none;
}
</style>
</head>

<body>
<div id="app" class="content">
<ul>
<li v-for="(i,k) in pageData">{{i}}</li>
</ul>
<div class="page_bar">
<a href="#" @click="pageTurning(0)">首页</a>
<a href="#" @click="pageTurning('before')">上一页</a>
<span v-if="ellipsis1">...</span>
<a href="#" class="page" v-for="(i,k) in max" :class="{active:idx==k}" v-if="range(k)" @click="pageTurning(k)" v-cloak="">{{k+1}}</a>
<span v-if="ellipsis2">...</span>
<a href="#" @click="pageTurning('next')">下一页</a>
<a href="#" @click="pageTurning(max-1)">尾页</a>
<span>第{{idx+1}}页</span>
<span>总页数{{max}}</span>
</div>
</div>

<script>
new Vue({
el: '#app',
data: {
data: [],
pageDataNum: 6,
idx: 0,
idxRange: 6,
ellipsis1: false,
ellipsis2: false
},
/*塞进所有的数据*/
mounted: function() {
for (var i = 0; i <= 88; i++) {
this.data.push(i);
}
},
methods: {
/*pageTurning---执行跳转页面*/
pageTurning: function(str) {

/*如果点击的是下一页,就将idx加1,得以刷新下一页数据*/
if (str === "next") {
if (this.idx < this.max - 1) {
this.idx++;
}
}
/*如果点击的是上一页,判断上一页的情况,如果上一页就是0-首页,不执行操作,否则就将idx减1,*/
else if (str === "before") {
if (this.idx > 0) {
this.idx--;
}
}
/*如果点击的是数字,比如第3页等,就将页数赋值给idx*/
else if (typeof str === "number") {
this.idx = str;
}
},
range: function(k) {
var idxStart;
var idxEnd;
if (!(this.idxRange % 2)) {
this.idxRange -= 1;
}
var x = this.idxRange / 2;

//尾部区间
if (this.idx > (this.max - (this.idxRange - 1))) {
idxStart = this.max - this.idxRange;
idxEnd = this.max - 1;
this.ellipsis1 = true;
this.ellipsis2 = false;
}
// 中间区间
if (this.idx > x && this.idx <= (this.max - (this.idxRange - 1))) {
idxStart = this.idx - x;
idxEnd = this.idx + x;
this.ellipsis1 = true;
this.ellipsis2 = true;
}
// 开始区间
if (this.idx < x) {
idxStart = 0;
idxEnd = this.idxRange;
this.ellipsis1 = false;
this.ellipsis2 = true;
}

if (idxStart && idxEnd) {
return k <= idxEnd && k >= idxStart;
} else {
return k < this.idxRange;
}
}
},
computed: {
/*max为13页*/
max: function() {
return Math.ceil(this.data.length / this.pageDataNum);
},
/*start为0,6*1,6*2*/
start: function() {
return this.idx * this.pageDataNum;
},
/*end为(0+1)*6,(1+1)*6,*/
end: function() {
return (this.idx + 1) * this.pageDataNum;
},
/*pageDate为6*/
pageData: function() {
return this.data.slice(this.start, this.end);
}
}

});
</script>
</body>

</html>

May 31, 2018
May 30, 2018
May 30, 2018