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

首頁 > 編程 > JavaScript > 正文

Angularjs實現分頁和分頁算法的示例代碼

2019-11-19 18:17:55
字體:
來源:轉載
供稿:網友

對于大多數web應用來說顯示項目列表是一種很常見的任務。通常情況下,我們的數據會比較多,無法很好地顯示在單個頁面中。在這種情況下,我們需要把數據以頁的方式來展示。

頁面展示效果:

頁面HTML代碼:

<table class="table table-striped" style="margin: 0px;">    <thead>     <tr>       <td>選擇</td>       <td>企業名稱</td>                      <td>企業地址</td>       <td>狀態</td>     </tr>   </thead>   <tbody>     <tr ng-repeat="l in list">       <td><input type="radio" name="id" ng-click="select(l.id)" value="{{l.id}}" /></td>       <td>{{l.name}}</td>       <td>{{l.address}}</td>       <td>{{l.status_str}}</td>     </tr>   </tbody> </table> <!-- paging --> <ul class="pagination" style="margin: 0px;" >     <li ng-class="{true:'disabled'}[p_current==1]"><a href="javascript:void(0);" ng-click="p_index()">首頁</a></li>     <li ng-repeat="page in pages" ng-class="{true:'active'}[p_current==page]"><a href="javascript:void(0);" ng-click="load_page(page)">{{ page }}</a></li>     <li ng-class="{true:'disabled'}[p_current==p_all_page]"><a href="javascript:void(0);" ng-click="p_last()">尾頁</a></li> </ul> <span style="vertical-align: 12px;"> 共:{{count}} 條</span> 

Js代碼:

var app = angular.module("myApp",[]);   app.controller("map_ctrl",function($scope,$http,$location){   //配置   $scope.count = 0;   $scope.p_pernum = 10;   //變量   $scope.p_current = 1;   $scope.p_all_page =0;   $scope.pages = [];   //初始化第一頁   _get($scope.p_current,$scope.p_pernum,function(){     alert("我是第一次加載");   });   //獲取數據   var _get = function(page,size,callback){     $http.get("xxx.html?status=0&page="+page+"&size="+size).success(function(res){       if(res&&res.status==1){         $scope.count=res.count;         $scope.list=res.list;         $scope.p_current = page;         $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);         reloadPno();         callback();       }else{         alert("加載失敗");       }     });    }   //單選按鈕選中   $scope.select= function(id){     alert(id);   }   //首頁   $scope.p_index = function(){     $scope.load_page(1);   }   //尾頁   $scope.p_last = function(){     $scope.load_page($scope.p_all_page);   }   //加載某一頁   $scope.load_page = function(page){     _get(page,$scope.p_pernum,function(){ });   };   //初始化頁碼   var reloadPno = function(){      $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,8);     }; //分頁算法 var calculateIndexes = function (current, length, displayLength) {   var indexes = [];   var start = Math.round(current - displayLength / 2);   var end = Math.round(current + displayLength / 2);   if (start <= 1) {     start = 1;     end = start + displayLength - 1;     if (end >= length - 1) {       end = length - 1;     }   }   if (end >= length - 1) {     end = length;     start = end - displayLength + 1;     if (start <= 1) {       start = 1;     }   }   for (var i = start; i <= end; i++) {     indexes.push(i);   }   return indexes;  };   }); 

分頁算法:

current :當前頁碼,length:總頁碼,displayLength:顯示長度      @return  array[1,2,3,4,5,6,7,8]     

var calculateIndexes = function (current, length, displayLength) {   var indexes = [];   var start = Math.round(current - displayLength / 2);   var end = Math.round(current + displayLength / 2);   if (start <= 1) {     start = 1;     end = start + displayLength - 1;     if (end >= length - 1) {       end = length - 1;     }   }   if (end >= length - 1) {     end = length ;     start = end - displayLength + 1;     if (start <= 1) {       start = 1;     }   }   for (var i = start; i <= end; i++) {     indexes.push(i);   }   return indexes; }; 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广水市| 沂水县| 聂拉木县| 绥宁县| 凌海市| 泾源县| 鹤山市| 长兴县| 潜江市| 德惠市| 盐边县| 新兴县| 巫溪县| 上饶县| 青龙| 宁都县| 汉中市| 宁国市| 民勤县| 灯塔市| 徐汇区| 平顶山市| 象州县| 屏山县| 新沂市| 阳原县| 阿拉善左旗| 融水| 合山市| 任丘市| 高青县| 建德市| 大埔县| 本溪市| 上杭县| 通江县| 麻城市| 花垣县| 文登市| 榆树市| 灵璧县|