angularjs實現(xiàn)上拉加載和下拉刷新數(shù)據(jù)功能
雖說AngularJS 1.x版本中對于上拉加載,下拉刷新數(shù)據(jù)功能都有做些封裝,但還是有些人不清楚。其實我一開始也是不懂的,so.現(xiàn)在把搞懂的記錄下免得少走彎路。
now,begin:先說下拉刷新吧,原理就是每次下拉都重新去服務器請求過一次新的數(shù)據(jù)。一般這種刷新功能的響應數(shù)據(jù)(也就是服務器返回的(json)數(shù)據(jù))中都會帶有
"rowsOfPage": 3,
"currentPage": 1,
"totalPages": 10,
"totalRows": 40,
"rowsOfPage":10,
"minRowNumber": 1,
"maxRowNumber": 3,
這樣的屬性字段。所以我們下拉刷新時只要把請求參數(shù)設置為currentPage:1,rowsOfPage:10。也就是要設置當前頁始終的值為1,一頁要顯示多少行。然后把返回的data保存在一個數(shù)組中,其實這樣基本就算是完成了這功能,但為了嚴謹些我們最好再判斷下這個數(shù)組的長度是否小于總條數(shù)。再在這判斷里面再判斷下這個數(shù)組長度是否等于0,如果是就說明沒有數(shù)據(jù)。我這邊就直接賦值一下下拉刷新的執(zhí)行代碼。
$scope.hasMore = false;
// $scope.dataNull=false; // 無數(shù)據(jù)提示
$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");
});
而在頁面中只要調用下<ion-refresher pulling-text="下拉刷新..." on-refresh="do_refresher()"></ion-refresher> 就可以了,其中$scope.$broadcast("scroll.refreshComplete");這個的作用是請求到數(shù)據(jù)刷新頁面。
接下來是上拉加載數(shù)據(jù)功能。這個會比下拉刷新麻煩一點,但都懂了話也還好。上拉加載原理理解:請求的currentPage參數(shù)值為累加1.把請求到數(shù)據(jù)用push方法循環(huán)加到已有數(shù)據(jù)的數(shù)組中。這是理想的數(shù)據(jù),我們平常在開發(fā)中還要判斷這個是否有數(shù)據(jù)加載。我就先上下代碼再說明應該會更好理解:
/*
* 上拉加載,分批加載服務端剩余的數(shù)據(jù)
*/
$scope.do_infinite = function() {
if (!$scope.hasMore) {
$scope.$broadcast("scroll.infiniteScrollComplete");
return;
}
// 如果當前頁數(shù)大于等于總頁數(shù),說明已經(jīng)沒數(shù)據(jù)可再加載了。
$scope.currentPage += 1;
ajax.post(reqUrl, {
"rowsOfPage": rowsOfPage,
"currentPage": $scope.currentPage
}, function(listdata, 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是布爾值判斷是否還有更多數(shù)據(jù)。然后在請求參數(shù)currentPage的值是用累加的。用for循環(huán)把返回的數(shù)據(jù)push到已有數(shù)據(jù)的數(shù)組中,再判斷當前的數(shù)組長度(也就是獲取到本地的總條數(shù))是否等于請求到返回數(shù)據(jù)總條數(shù)屬性的值。如果這布爾值為true說明還有數(shù)據(jù)。同上 $scope.$broadcast("scroll.infiniteScrollComplete"); 也是刷新頁面數(shù)據(jù)。在頁面中只要在ion-list下面添加<ion-infinite-scroll ng-if="hasMore" on-infinite="do_infinite()" immediate-check="false"></ion-infinite-scroll> 就可以執(zhí)行。
note:在html頁面中,下拉刷新的功能要放在ion-list上面
,
上拉加載則放在ion-list下面
有圖片總不會理解錯了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用Angular CLI生成 Angular 5項目教程詳解
這篇文章主要介紹了使用Angular CLI生成 Angular 5項目的教程詳解 ,需要的朋友可以參考下2018-03-03
angular使用md5,CryptoJS des加密的方法
這篇文章主要介紹了angular使用md5,CryptoJS des加密的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06
AngularJS基礎 ng-mouseenter 指令示例代碼
本文主要介紹AngularJS ng-mouseenter 指令,這里對ng-mouseenter 指令基礎資料做了詳細整理,并附代碼實例,有需要的小伙伴可以參考下2016-08-08

