vue 實現分頁
效果圖:
圖有點醜,請不要見怪,剛學這個東西,能實現效果纔是關鍵。
圖中還有 添加 修改和刪除的方法,我沒有寫在下方,下面的方法只是分頁的方法
dao層:
service層:
controller頁面:
注意:不要導錯包!!!
HTML頁面:
data數據:
方法:
計算屬性:
dao層:
//分頁表連接
@Query(value="SELECT * FROM person JOIN cla ON person.`cid`=cla.`ccid` ORDER BY person.`id` DESC",nativeQuery=true)
Page<List<Map>> findAllMap(Pageable page);
service層:
//分頁表連接
public Page<List<Map>> findAllMap(Pageable page){
return personDao.findAllMap(page);
}
controller層:
//實現分頁
@RequestMapping("pageList")
@ResponseBody
public Page<List<Map>> pageList(HttpServletRequest request){
Integer pageSize=Integer.parseInt(request.getParameter("pageSize")); //每頁顯示的條數
Integer NowPage=Integer.parseInt(request.getParameter("NowPage")); //傳遞參數
Pageable Page=PageRequest.of(NowPage-1,pageSize);
Page<List<Map>> data = personService.findAllMap(Page);
return 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>
CSS樣式:
/*分頁*/
.page-bar{
margin:40px auto;
margin-top: 20px;
}
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;
}
JS代碼:
<script type="text/javascript">
var vm=new Vue({
el: '#app', //定位到某個標籤區域
data: {
sites:[], //定義一個數組
all: 10, //總頁數
cur: 1,//當前頁碼
totalPage: 0,//當前條數
},
mounted(){
//mounted:在模板渲染成html後調用,通常是初始化頁面完成後,再對html的dom節點進行一些需要的操作。
this.dataListFn(1); //默認爲第一頁
},
methods:{ //相當於js中定義函數
//分頁查詢方法
dataListFn:function(index){
var page=index;
$.post("/person/pageList",{NowPage:page,pageSize:5},function(data){
vm.sites=[];
//alert(JSON.stringify(data));
var len=data.content.length;
var dataList=data.content; //獲取數據列表
for(var i=0;i<len;i++){
vm.sites.push(dataList[i]);
}
vm.all=data.totalPages; //總頁數
vm.cur=data.number+1; //當前頁面
vm.totalPage=data.numberOfElements; //當前條數
})
},
//分頁
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
}
}
});
</script>
···············································································································································