当前位置: 移动技术网 > IT编程>开发语言>JavaScript > vue实现分页

vue实现分页

2019年04月26日  | 移动技术网IT编程  | 我要评论

html页面:

data数据:

方法:

计算属性:

样式:

 

html代码:

<!--分页-->
<div class="page-bar">
<ul>
<li v-if="cur>1"><a v-on:click="cur--,pageclick()">上一页</a></li>
<li v-if="cur==1"><a class="banclick">上一页</a></li>
<li v-for="index in indexs" v-bind:class="{ 'active': cur == index}">
<a v-on:click="btnclick(index)">{{ index }}</a>
</li>
<li v-if="cur!=all"><a v-on:click="cur++,pageclick()">下一页</a></li>
<li v-if="cur == all"><a class="banclick">下一页</a></li>
<li><a>共<i>{{all}}</i>页</a></li>
</ul>
</div>

===========================================================================

js代码:

data () {
return {
all: 10, //总页数
cur: 1,//当前页码
totalpage: 0,//当前条数
}
},
methods: {
//请求数据
datalistfn: function(index){
this.$axios.get("http://127.0.0.1:8090/demand/selectlistbypage",
{
params:{
page: index,
limit:'10',
state: 0
}
}).then((res) => {
if(res.data.message == "success"){
this.datalist=[];
for(let i=0;i<res.data.data.length;i++){
this.datalist.push(res.data.data[i])
}
this.all = res.data.totalpage;//总页数
this.cur = res.data.pagenum;
this.totalpage = res.data.totalpage;
}

});
},
//分页
btnclick: function(data){//页码点击事件
if(data != this.cur){
this.cur = data
}
//根据点击页数请求数据
this.datalistfn(this.cur.tostring());
},
pageclick: function(){
//根据点击页数请求数据
this.datalistfn(this.cur.tostring());
}
},
computed: {
//分页
indexs: function(){
var left = 1;
var right = this.all;
var ar = [];
if(this.all>= 5){
if(this.cur > 3 && this.cur < this.all-2){
left = this.cur - 2
right = this.cur + 2
}else{
if(this.cur<=3){
left = 1
right = 5
}else{
right = this.all
left = this.all -4
}
}
}
while (left <= right){
ar.push(left)
left ++
}
return ar
}
}

================================================================================

css代码:

/*分页*/
.page-bar{
margin:40px auto;
margin-top: 150px;

}
ul,li{
margin: 0px;
padding: 0px;
}
li{
list-style: none
}
.page-bar li:first-child>a {
margin-left: 0px
}
.page-bar a{
border: 1px solid #ddd;
text-decoration: none;
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #5d6062;
cursor: pointer;
margin-right: 20px;
}
.page-bar a:hover{
background-color: #eee;
}
.page-bar a.banclick{
cursor:not-allowed;
}
.page-bar .active a{
color: #fff;
cursor: default;
background-color: #e96463;
border-color: #e96463;
}
.page-bar i{
font-style:normal;
color: #d44950;
margin: 0px 4px;
font-size: 12px;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网