angularjs 上拉加载,下拉刷新数据功能
虽说angularjs 1.x版本中对于上拉加载,下拉刷新数据功能都有做些封装,但还是有些人不清楚。其实我一开始也是不懂的,so.现在把搞懂的记录下免得少走弯路。 "rowsOfPage": 3,"currentPage": 1,"totalPages": 10,"totalRows": 40,"rowsOfPage":10,"minRowNumber": 1,"maxRowNumber": 3,
这样的属性字段。所以我们下拉刷新时只要把请求参数设置为currentPage:1,rowsOfPage:10。也就是要设置当前页始终的值为1,一页要显示多少行。然后把返回的data保存在一个数组中,其实这样基本就算是完成了这功能,但为了严谨些我们最好再判断下这个数组的长度是否小于总条数。再在这判断里面再判断下这个数组长度是否等于0,如果是就说明没有数据。我这边就直接赋值一下下拉刷新的执行代码。 $scope.hasMore = false;
// $scope.dataNull=false; // 无数据提示
$scope.SName = "您当前没有待办事务";
$scope.do_refresher = function() {
$scope.currentPage = 1;
$scope.bItems = [];
ajax.post(reqUrl,{
"rowsOfPage": rowsOfPage,"currentPage": $scope.currentPage
},function(listdata,successful) {
if (successful) {
$scope.bItems = listdata.datas || [];
$scope.hasMore = ($scope.bItems.length < listdata.totalRows);
if ($scope.bItems.length == 0) {
$scope.dataNull = true;
} else {
$scope.dataNull = false;
}
} else {
$scope.hasMore = false;
}
$scope.$broadcast("scroll.refreshComplete");
});
而在页面中只要调用下 接下来是上拉加载数据功能。这个会比下拉刷新麻烦一点,但都懂了话也还好。上拉加载原理理解:请求的currentPage参数值为累加1.把请求到数据用push方法循环加到已有数据的数组中。这是理想的数据,我们平常在开发中还要判断这个是否有数据加载。我就先上下代码再说明应该会更好理解: /* * 上拉加载,分批加载服务端剩余的数据 */
$scope.do_infinite = function() {
if (!$scope.hasMore) {
$scope.$broadcast("scroll.infiniteScrollComplete");
return;
}
// 如果当前页数大于等于总页数,说明已经没数据可再加载了。
$scope.currentPage += 1;
ajax.post(reqUrl,successful) {
if (successful) {
//window.debug && alert("length " + listdata.datas.length + " yeshu " + $scope.currentPage);
$scope.currentPage = listdata.currentPage;
for (var i = 0; i < listdata.datas.length; i++) {
$scope.bItems.push(listdata.datas[i]);
}
$scope.hasMore = ($scope.bItems.length < listdata.totalRows);
} else {
$scope.hasMore = false;
}
$scope.$broadcast("scroll.infiniteScrollComplete");
});
其中hasmore是布尔值判断是否还有更多数据。然后在请求参数currentPage的值是用累加的。用for循环把返回的数据push到已有数据的数组中,再判断当前的数组长度(也就是获取到本地的总条数)是否等于请求到返回数据总条数属性的值。如果这布尔值为true说明还有数据。同上 note:在html页面中,下拉刷新的功能要放在ion-list上面,上拉加载则放在ion-list下面 有图片总不会理解错了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |