国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angularjs 實現分頁功能及示例代碼

2019-11-20 09:00:24
字體:
來源:轉載
供稿:網友

基于Angularjs實現分頁

前言

       學習任何一門語言前肯定是有業務需求來驅動你去學習它,當然ng也不例外,在學習ng前我第一個想做的demo就是基于ng實現分頁,除去基本的計算思路外就是使用指令封裝成一個插件,在需要分頁的列表頁面內直接引用。

插件

      在封裝分頁插件時我實現了幾種方式總體都比較零散,最后找到了一個朋友(http://www.miaoyueyue.com/archives/813.html)封裝的插件,覺還不錯,讀了下他的源碼就直接在項目中使用了。

原理和使用說明

      1、插件源碼主要基于angular directive來實現。

      2、調用時關鍵地方是后臺請求處理函數,也就是從后臺取數據。

      3、插件有兩個關鍵參數currentPage、itemsPerPage,當前頁碼和每頁的記錄數。

      4、實現方法調用后我們需要根據每次點擊分頁插件頁碼時重新提交后臺來獲取相應頁碼數據。 在調用的頁碼中我使用了$watch來監控。  我初次使用時是把調用函數放在了插件的onchange中,結果發現每次都會觸發兩次后臺。這個地方需要注意。

      5、我把請求后臺封裝成了Service層,然后在Controller里調用,也符合MVC思想。

效果圖


 

調用代碼

<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;   });   }   //配置分頁基本參數  $scope.paginationConf = {   currentPage: 1,   itemsPerPage: 5  };   /***************************************************************  當頁碼和頁面記錄數發生變化時監控后臺查詢  如果把currentPage和itemsPerPage分開監控的話則會觸發兩次后臺事件。  ***************************************************************/  $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetAllEmployee); }]);   //業務類 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 實現分頁功能的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥中县| 时尚| 乐都县| 临邑县| 华阴市| 瑞金市| 杭锦后旗| 望都县| 临泽县| 屏南县| 临潭县| 外汇| 景谷| 娱乐| 凤山县| 西充县| 洛川县| 大渡口区| 沾化县| 阿图什市| 定安县| 拉萨市| 莒南县| 安新县| 固镇县| 镇远县| 临汾市| 濮阳市| 西丰县| 吉首市| 那曲县| 思南县| 施甸县| 边坝县| 京山县| 平原县| 岳池县| 黄大仙区| 隆化县| 宁武县| 沾化县|