Spring boot+Vue分页

实现效果图片

在这里插入图片描述

Html代码:

<div id="app">
<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>
<form action="" method="post">
<table border="1px" width="100%" style="text-align: center" >
	<tr>
		<td>ID</td>
		<td>NAME</td>
		<td>CELL</td>
		<td>ADDRESS</td>
		<td>TEXT</td>
		
		<td>OPERATION</td>
		<td>ALL<input type="checkbox" id="quan"/>/BACK<input  type="checkbox" id="fan"/></td>
	</tr>
	<tr v-for="(stu,key) in stu">
		<td>{{key+1}}</td>
		<td>{{stu.sname}}</td>
		<td>{{stu.cell}}</td>
		<td>{{stu.province+stu.market+stu.country}}</td>
		<td>{{stu.text}}</td>
		
		<td><span v-on:click="upd(stu.id)">修改</span>/<span v-on:click="del(stu.id,key)">删除</span></td>
		<td><input type="checkbox" name="che" id="che"/></td>
	</tr>
	<tr>
		<td colspan="6"><a href="">添加</a></td>
		<td><input type="submit" value="删除"/></td>
	</tr>
</table>
</form>
</div>

css代码:

<style type="text/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;
}
</style>

vue代码

var vm = new Vue({
	el:'#app',
	data:{
		stu:[],
		all: 10, //总页数
		cur: 1,//当前页码
		totalPage: 0,//当前条数
		sites:[]
	},
	mounted () {
		this.dataListFn(1);//初始化页数
	},
	methods : {
		del : function(id,key){
			$.post( "/student/delete" , {id:id} , function(data) {
				if(data=="1"){
					vm.stu.splice(key,1);
					alert("删除成功!!!");
				}
			});
		},
		upd:function(id){
			window.location.href="/student/studentUpdate?id="+id;
		},
		 dataListFn:function(index){
				var page = index;
				var limit= 10;
				var state= 0;
			 $.post("/student/page",{NewPage:page},function(data){
				 alert(JSON.stringify(data)); 
				 vm.stu=[];
				 var len  = data.content.length;
				 var dataList = data.content;
				 for (var i = 0; i < len; i++) {
					vm.stu.push(dataList[i]);
				}
					vm.all = data.totalPages;//总页数
					vm.cur = data.number+1;
					vm.totalPage = data.numberOfElements;
			 });
		 },
		//分页
		 btnClick: function(data){//页码点击事件
		 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
		 }
		 }
});

java代码:
控制层代码

@RequestMapping("page")
	@ResponseBody
	public Page<Student> page(HttpServletRequest request){
		Sort sort=null;
		sort=new Sort(Sort.Direction.DESC,"id");//排序(按id倒叙)
		Integer NewPage=Integer.parseInt(request.getParameter("NewPage"));
		Pageable page=PageRequest.of(NewPage-1, 5,sort);
		Page<Student> data=studentService.findAll(page);
		return data;
	}

Pageable page=PageRequest.of(NewPage-1, 5,sort);

(NewPage-1, 5,sort):NewPage-1:当前页数,因为分页的页数和数组下标一样是从零来时的,所以这里要减一;
5是每一页的条数;
sort:是排序的方式这里我采用的是倒叙,如果是想要正序则将Sort(Sort.Direction.DESC,“id”)中的DESC改成ASC

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值