Angularjs 實(shí)現(xiàn)分頁功能及示例代碼
基于Angularjs實(shí)現(xiàn)分頁
前言
學(xué)習(xí)任何一門語言前肯定是有業(yè)務(wù)需求來驅(qū)動(dòng)你去學(xué)習(xí)它,當(dāng)然ng也不例外,在學(xué)習(xí)ng前我第一個(gè)想做的demo就是基于ng實(shí)現(xiàn)分頁,除去基本的計(jì)算思路外就是使用指令封裝成一個(gè)插件,在需要分頁的列表頁面內(nèi)直接引用。
插件
在封裝分頁插件時(shí)我實(shí)現(xiàn)了幾種方式總體都比較零散,最后找到了一個(gè)朋友(http://www.miaoyueyue.com/archives/813.html)封裝的插件,覺還不錯(cuò),讀了下他的源碼就直接在項(xiàng)目中使用了。
原理和使用說明
1、插件源碼主要基于angular directive來實(shí)現(xiàn)。
2、調(diào)用時(shí)關(guān)鍵地方是后臺(tái)請求處理函數(shù),也就是從后臺(tái)取數(shù)據(jù)。
3、插件有兩個(gè)關(guān)鍵參數(shù)currentPage、itemsPerPage,當(dāng)前頁碼和每頁的記錄數(shù)。
4、實(shí)現(xiàn)方法調(diào)用后我們需要根據(jù)每次點(diǎn)擊分頁插件頁碼時(shí)重新提交后臺(tái)來獲取相應(yīng)頁碼數(shù)據(jù)。 在調(diào)用的頁碼中我使用了$watch來監(jiān)控。 我初次使用時(shí)是把調(diào)用函數(shù)放在了插件的onchange中,結(jié)果發(fā)現(xiàn)每次都會(huì)觸發(fā)兩次后臺(tái)。這個(gè)地方需要注意。
5、我把請求后臺(tái)封裝成了Service層,然后在Controller里調(diào)用,也符合MVC思想。
效果圖

調(diào)用代碼
<div ng-app="DemoApp" ng-controller="DemoController">
<table class="table table-striped">
<thead>
<tr>
<td>ID</td>
<td>FirstName</td>
<td>LastName</td>
<td>Status</td>
<td>Address</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="emp in persons">
<td>{{emp.ID}}</td>
<td>{{emp.FirstName}}</td>
<td>{{emp.LastName}}</td>
<td>{{emp.Status}}</td>
<td>{{emp.Address}}</td>
</tr>
</tbody>
</table>
<tm-pagination conf="paginationConf"></tm-pagination>
</div>
<script type="text/javascript">
var app = angular.module('DemoApp', ['tm.pagination']);
app.controller('DemoController', ['$scope', 'BusinessService', function ($scope, BusinessService) {
var GetAllEmployee = function () {
var postData = {
pageIndex: $scope.paginationConf.currentPage,
pageSize: $scope.paginationConf.itemsPerPage
}
BusinessService.list(postData).success(function (response) {
$scope.paginationConf.totalItems = response.count;
$scope.persons = response.items;
});
}
//配置分頁基本參數(shù)
$scope.paginationConf = {
currentPage: 1,
itemsPerPage: 5
};
/***************************************************************
當(dāng)頁碼和頁面記錄數(shù)發(fā)生變化時(shí)監(jiān)控后臺(tái)查詢
如果把currentPage和itemsPerPage分開監(jiān)控的話則會(huì)觸發(fā)兩次后臺(tái)事件。
***************************************************************/
$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetAllEmployee);
}]);
//業(yè)務(wù)類
app.factory('BusinessService', ['$http', function ($http) {
var list = function (postData) {
return $http.post('/Employee/GetAllEmployee', postData);
}
return {
list: function (postData) {
return list(postData);
}
}
}]);
</script>
插件和Demo下載
http://yunpan.cn/cQEhnLrpnkniQ 訪問密碼 be74
以上就是AngularJS 實(shí)現(xiàn)分頁功能的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解
這篇文章主要介紹了使用Angular CLI進(jìn)行Build(構(gòu)建)和Serve詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
AngularJS 文件上傳控件 ng-file-upload詳解
這篇文章主要介紹了AngularJS 文件上傳控件 ng-file-upload詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
詳解使用angular-cli發(fā)布i18n多國語言Angular應(yīng)用
這篇文章主要介紹了詳解使用angular-cli發(fā)布i18n多國語言Angular應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
解決angular雙向綁定無效果,ng-model不能正常顯示的問題
今天小編就為大家分享一篇解決angular雙向綁定無效果,ng-model不能正常顯示的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
angular.foreach 循環(huán)方法使用指南
本文主要介紹了angular.foreach 循環(huán)方法使用格式及參數(shù),是篇非?;A(chǔ)的文章,與需要的小伙伴參考下2015-01-01
Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐
本篇文章主要介紹了Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

