您好,登錄后才能下訂單哦!
這篇文章主要介紹angularjs如何實(shí)現(xiàn)上拉加載和下拉刷新數(shù)據(jù)功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1、能夠嵌入動(dòng)態(tài)文本于HTML頁面。2、對(duì)瀏覽器事件做出響應(yīng)。3、讀寫HTML元素。4、在數(shù)據(jù)被提交到服務(wù)器之前驗(yàn)證數(shù)據(jù)。5、檢測(cè)訪客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。7、基于Node.js技術(shù)進(jìn)行服務(wù)器端編程。
雖說AngularJS 1.x版本中對(duì)于上拉加載,下拉刷新數(shù)據(jù)功能都有做些封裝,但還是有些人不清楚。其實(shí)我一開始也是不懂的,so.現(xiàn)在把搞懂的記錄下免得少走彎路。
now,begin:先說下拉刷新吧,原理就是每次下拉都重新去服務(wù)器請(qǐng)求過一次新的數(shù)據(jù)。一般這種刷新功能的響應(yīng)數(shù)據(jù)(也就是服務(wù)器返回的(json)數(shù)據(jù))中都會(huì)帶有
"rowsOfPage": 3, "currentPage": 1, "totalPages": 10, "totalRows": 40, "rowsOfPage":10, "minRowNumber": 1, "maxRowNumber": 3,
這樣的屬性字段。所以我們下拉刷新時(shí)只要把請(qǐng)求參數(shù)設(shè)置為currentPage:1,rowsOfPage:10。也就是要設(shè)置當(dāng)前頁始終的值為1,一頁要顯示多少行。然后把返回的data保存在一個(gè)數(shù)組中,其實(shí)這樣基本就算是完成了這功能,但為了嚴(yán)謹(jǐn)些我們最好再判斷下這個(gè)數(shù)組的長(zhǎng)度是否小于總條數(shù)。再在這判斷里面再判斷下這個(gè)數(shù)組長(zhǎng)度是否等于0,如果是就說明沒有數(shù)據(jù)。我這邊就直接賦值一下下拉刷新的執(zhí)行代碼。
$scope.hasMore = false; // $scope.dataNull=false; // 無數(shù)據(jù)提示 $scope.SName = "您當(dāng)前沒有待辦事務(wù)"; $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"); });
而在頁面中只要調(diào)用下<ion-refresher pulling-text="下拉刷新..." on-refresh="do_refresher()"></ion-refresher> 就可以了,其中$scope.$broadcast("scroll.refreshComplete");這個(gè)的作用是請(qǐng)求到數(shù)據(jù)刷新頁面。
接下來是上拉加載數(shù)據(jù)功能。這個(gè)會(huì)比下拉刷新麻煩一點(diǎn),但都懂了話也還好。上拉加載原理理解:請(qǐng)求的currentPage參數(shù)值為累加1.把請(qǐng)求到數(shù)據(jù)用push方法循環(huán)加到已有數(shù)據(jù)的數(shù)組中。這是理想的數(shù)據(jù),我們平常在開發(fā)中還要判斷這個(gè)是否有數(shù)據(jù)加載。我就先上下代碼再說明應(yīng)該會(huì)更好理解:
/* * 上拉加載,分批加載服務(wù)端剩余的數(shù)據(jù) */ $scope.do_infinite = function() { if (!$scope.hasMore) { $scope.$broadcast("scroll.infiniteScrollComplete"); return; } // 如果當(dāng)前頁數(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ù)。然后在請(qǐng)求參數(shù)currentPage的值是用累加的。用for循環(huán)把返回的數(shù)據(jù)push到已有數(shù)據(jù)的數(shù)組中,再判斷當(dāng)前的數(shù)組長(zhǎng)度(也就是獲取到本地的總條數(shù))是否等于請(qǐng)求到返回?cái)?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下面 有圖片總不會(huì)理解錯(cuò)了。
以上是“angularjs如何實(shí)現(xiàn)上拉加載和下拉刷新數(shù)據(jù)功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。