对照着jquery来学vue.js系列之配合thinkphp下拉获取分页数据
上篇文章介绍了vue.js如何ajax获取数据;
接着不可避免就遇到的是;
如何进行数据分页呢?
这里以thinkphp为示例讲解;其他场景性质一样;
示例项目:http://git.oschina.net/shuaibai123/thinkphp-bjyadmin
示例链接:localhost/Home/Vue/web_page
项目中有张表province_city_area;
里面是全国的3000多个城市;这里就拿它做分页了;
一:thinkphp获取分页数据/Application/Home/Controller/VueController.class.php<br>
/**<br>
* 配合thinkphp分页示例<br>
*/<br>
public function page(){<br>
// 获取总条数<br>
$count=M('Province_city_area')->count();<br>
// 每页多少条数据<br>
$limit=100;<br>
$page=new \Org\Nx\Page($count,$limit);<br>
$data=M('Province_city_area')<br>
->limit($page->firstRow.','.$page->listRows)<br>
->select();<br>
echo json_encode($data);<br>
}
二:前端接收数据的核心部分;
要实现的是移动端往那种下拉就加载数据的效果;
首先是先用ready方法加载第一页的数据显示到页面中;
设置一个变量i=1;var vm=new Vue({<br>
el: '.box',<br>
data: {<br>
city: []<br>
},<br>
ready: function(){<br>
this.$http.get(url).then(function(response){<br>
this.city=response.data;<br>
})<br>
},<br>
})
然后呢;判断当滚动轴到底部的时候;
让i+1 作为get参数中的页数;
加载下一页的数据;并追加到city中;i++<br>
vm.$http.get(pageData.url+'/p/'+pageData.i).then(function(response){<br>
// 用concat把下一页的数据追加到city中<br>
vm.city=vm.city.concat(response.data); <br>
})
三:完整的html;
/tpl/Home/Vue/web_page.html<!DOCTYPE html><br>
<html><br>
<head><br>
<meta charset="UTF-8"><br>
<title>Vue 配合thinkphp分页示例</title><br>
</head><br>
<body><br>
<p></p><br>
<p></p><br>
<div><br>
<p v-for="item in city">{{item.name}}</p><br>
</div><br>
<br>
<p style="display: none;">No data</p><br>
<br>
<vue /><br>
<script><br>
<br>
//Get the url of the data<br>
var pageData={<br>
URL: "{:U('Home/Vue/page')}",<br>
i: 1,<br>
height: 0,<br>
Over: false<br>
}<br>
var vm=new Vue({<br>
el: '.box',<br>
Data: {<br>
City: []<br>
},<br>
ready: function(){<br>
This.$http.get(pageData.url).then(function(response){<br>
This.city=response.data;<br>
})<br>
},<br>
})<br>
<br>
<br>
//Get the current position of the scroll bar <br>
function getScrollTop() { <br>
var scrollTop=0; <br>
If(document.documentElement && document.documentElement.scrollTop){ <br>
scrollTop=document.documentElement.scrollTop; <br>
}else if(document.body) { <br>
scrollTop=document.body.scrollTop; <br>
} <br>
Return scrollTop; <br>
} <br>
<br>
//Get the height of the current visual range <br>
function getClientHeight() { <br>
var clientHeight=0; <br>
If(document.body.clientHeight && document.documentElement.clientHeight){ <br>
clientHeight=Math.min(document.body.clientHeight, document.documentElement.clientHeight); <br>
}else{ <br>
clientHeight=Math.max(document.body.clientHeight, document.documentElement.clientHeight); <br>
} <br>
Return clientHeight; <br>
} <br>
<br>
//Get the complete height of the document <br>
function getScrollHeight() { <br>
Return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight); <br>
} <br>
<br>
//Add loading style<br>
function addLoading(){<br>
var loading=document.createElement('p');<br>
Loading.className='loading'<br>
Loading.innerHTML='Loading...';<br>
Document.body.appendChild(loading);<br>
}<br>
<br>
//Delete loading style<br>
function removeLoading(){<br>
var loading=document.querySelector('.loading');<br>
Loading.parentNode.removeChild(loading);<br>
}<br>
<br>
// Change Loading to No Data<br>
function loadingToOver(){<br>
var loading=document.querySelector('.over');<br>
Loading.style.display='block';<br>
}<br>
<br>
//Listen to scroll events<br>
window.onscroll=function () {<br>
If (pageData.over) {<br>
return return false;
}<br>
If (getScrollHeight()-(getScrollTop()+getClientHeight())<=50) {<br />
// Number of pages +1<br />
pageData.i++<br />
//Show loading<br />
addLoading();<br />
// Get the data on the next page<br />
vm.$http.get(pageData.url+'/p/'+pageData.i).then(function(response){<br />
removeLoading();<br />
If(response.data.length==0){<br />
pageData.over=true;<br />
loadingToOver();<br /> }else{<br />
vm.city=vm.city.concat(response.data); <br />
}<br />
})<br />
} <br />
} <br />
<br />
<br />
</script><br>
</body><br>
</html>
This has simply implemented the function of drop-down loading data;
Don’t be scared by the long code above;
More of them use native js to implement scroll axis monitoring events;
Just design the loading and loading styles according to the business;
Got it; I’ve been very out of sorts recently due to some things;
This article that has been delayed for many days is finally finished;
View the source code and comments directly for more information;
Bai Junyao's blog http://baijunyao.com/article/88