Angularjs 實現(xiàn)分頁功能及示例代碼
基于Angularjs實現(xiàn)分頁
前言
學習任何一門語言前肯定是有業(yè)務需求來驅(qū)動你去學習它,當然ng也不例外,在學習ng前我第一個想做的demo就是基于ng實現(xiàn)分頁,除去基本的計算思路外就是使用指令封裝成一個插件,在需要分頁的列表頁面內(nèi)直接引用。
插件
在封裝分頁插件時我實現(xiàn)了幾種方式總體都比較零散,最后找到了一個朋友(http://www.miaoyueyue.com/archives/813.html)封裝的插件,覺還不錯,讀了下他的源碼就直接在項目中使用了。
原理和使用說明
1、插件源碼主要基于angular directive來實現(xiàn)。
2、調(diào)用時關(guān)鍵地方是后臺請求處理函數(shù),也就是從后臺取數(shù)據(jù)。
3、插件有兩個關(guān)鍵參數(shù)currentPage、itemsPerPage,當前頁碼和每頁的記錄數(shù)。
4、實現(xiàn)方法調(diào)用后我們需要根據(jù)每次點擊分頁插件頁碼時重新提交后臺來獲取相應頁碼數(shù)據(jù)。 在調(diào)用的頁碼中我使用了$watch來監(jiān)控。 我初次使用時是把調(diào)用函數(shù)放在了插件的onchange中,結(jié)果發(fā)現(xiàn)每次都會觸發(fā)兩次后臺。這個地方需要注意。
5、我把請求后臺封裝成了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 }; /*************************************************************** 當頁碼和頁面記錄數(shù)發(fā)生變化時監(jiān)控后臺查詢 如果把currentPage和itemsPerPage分開監(jiān)控的話則會觸發(fā)兩次后臺事件。 ***************************************************************/ $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetAllEmployee); }]); //業(yè)務類 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 實現(xiàn)分頁功能的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
AngularJS控制器controller正確的通信的方法
AngularJS中的controller是個函數(shù),用來向視圖的作用域($scope)添加額外的功能,我們用它來給作用域?qū)ο笤O置初始狀態(tài),并添加自定義行為2016-01-01Angular.JS利用ng-disabled屬性和ng-model實現(xiàn)禁用button效果
這篇文章主要介紹了Angular.JS利用ng-disabled屬性和ng-model實現(xiàn)禁用button效果的相關(guān)資料,文中給出了詳細的示例代碼,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04angular6.0使用教程之父組件通過url傳遞id給子組件的方法
這篇文章主要介紹了angular6.0使用教程之父組件通過url傳遞id給子組件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06AngularJS遞歸指令實現(xiàn)Tree View效果示例
這篇文章主要介紹了AngularJS遞歸指令實現(xiàn)Tree View效果,結(jié)合實例形式分析了AngularJS基于遞歸指令實現(xiàn)樹形結(jié)構(gòu)層次數(shù)據(jù)的相關(guān)操作步驟與注意事項,需要的朋友可以參考下2016-11-11