spring boot+vue 实现分页功能
效果图
**
前端部分
**
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>
JavaScript
<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>
vue
<script type="text/javascript">
var vm=new Vue({
el: "#root",
data: {
all: 10, //总页数
cur: 1,//当前页码
totalPage: 0,//当前条数
},
mounted() {
this.getStudentDate(1);
},
methods: {
getStudentDate: function (page) {
if (page == null || page == "") {
page = 1;
}
$.ajax({
url: 'http://localhost:8080/student/index?page=' + page,
type: 'post',
data: this.like,
dataType: "json",
success: function (result) {
// alert(JSON.stringify(result));
if (result.code == 0) {
vm.list = [];
var len=result.data.content.length;
for (var i = 0; i < len; i++) {
vm.list.push(result.data.content[i]);
}
vm.all = result.data.totalPages;//总页数
vm.cur = result.data.number + 1;//当前页
//data.pageable.pageNumber
vm.totalPage = result.data.numberOfElements;//当前条数
} else {
vm.message = result.msg;
}
}
})
},
//分页
btnClick: function(data){//页码点击事件
if(data != this.cur){
this.cur = data
}
//根据点击页数请求数据
this.getStudentDate(this.cur.toString());
},
pageClick: function(){
//根据点击页数请求数据
this.getStudentDate(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
}
}
})
</script>
后端部分
controller.java
@RequestMapping("/index")
@ResponseBody
public JsonResult index(Student student, HttpServletRequest request){
//List<Student> data= studentService.findAll(Sort.by("sid"));
// System.out.print(request.getParameter("name"));
// System.out.print(request.getParameter("sid"));
// System.out.print(request.getParameter("telephone"));
int page=Integer.parseInt(request.getParameter("page"));
System.out.println(Integer.parseInt(request.getParameter("page")));
Page<Student> data= serviceStudent.queryFlows(page,5,student);
return new JsonResult<>(data, "获取学生列表成功");
}
ServiceStudent.java