星空网 > 软件开发 > Java

用angularJs实现分页功能,不带省略号。

angularJs 的分页重点体现在对 过滤器 的使用。这个过滤器也并不复杂。

首先上 html 代码:

 1 <!DOCTYPE html> 2 <html ng-app="demoApp"> 3 <head> 4  <meta charset="utf-8"> 5  <meta name="viewport" content="width=device-width"> 6  <title>demo</title> 7  <link rel="stylesheet" href="demo.css"> 8 </head> 9 <body>10  <div ng-controller="demoCtrl">11   <div>12    <ul>13     <li ng-repeat="sentences in demoLists[0].name | paging:currentPage*listsPerPage | limitTo:listsPerPage">{{sentences}}</li> <!-- ng-repeat 动态生成模拟的数据 -->14    </ul>15   </div>16   <div>17    <a class="step prevLink" ng-click="prevPage()">上一页</a>18    <a ng-class="{true:'currentStep',false:'step'}[num==currentPage]" ng-repeat="num in pageNum" ng-click="setPage(num)">{{num+1}}</a> <!-- ng-repeat 动态生成页码 -->19    <a class="step nextLink" ng-click="nextPage()">下一页</a>20   </div>21  </div>22  <script src='/images/loading.gif' data-original="angular.min.js"></script> <!-- 引入你的 angularJs 文件 -->23  <script src='/images/loading.gif' data-original="demo.js"></script>24 </body>25 </html>

 

这里面用到了 ng-class,当前页 currentPage 等于页码 num 时,显示 currentStep 的样式,不等于时显示 step 的样式。

重点代码在 13 行,ng-repeat 模拟数据的时候加了过滤器,过滤器名字叫 paging 和一个 angular 自带的过滤 limitTo。

 

然后是 css 代码,没有什么可说的,主要是调样式。其中记得加上 ng-class 里的两个样式。

 1 ul>li{ 2   list-style:none; 3   width:300px; 4   height:40px; 5   border:1px solid #4CAF50; 6   margin-bottom:4px; 7   padding-left:5px; 8 } 9 .nextLink,.prevLink{10   font-size: 12px;11    line-height: 24px;12   height: 24px;13   border: solid 1px #aaa;14   color: #999;15   padding: 0 9px;16   margin: 0 0 0 5px;17   list-style: none;18   background: #f2f2f2;19   float: left;20   cursor: pointer;21 }22 23 a.prevLink:hover,a.nextLink:hover {24   background: #aaa !important;25   color: #fff !important;26   cursor: pointer;27 }28 29 .step {30   display: block;31   line-height: 24px;32   height: 24px;33   border: solid 1px #aaa;34   color: #999;35   background: #fff;36   padding: 0 9px;37   font-size: 12px;38   float: left;39   margin: 0 0 0 5px;40   list-style: none;41   cursor: pointer;42 }43 .currentStep{44   border-color: #fff;45   padding: 0 4px;46   color: #f90;47   font-weight: bold;48   float: left;49   display: block;50   line-height: 24px;51   height: 24px;52   padding: 0 9px;53   font-size: 12px;54   float: left;55   margin: 0 0 0 5px;56   list-style: none;57   cursor: pointer;58 }

 

最后就是 js 了

 1 var demoApp = angular.module('demoApp',[]); 2 demoApp.filter('paging',function(){   //paging 过滤器 3  return function(lists,start){   //两个参数 lists 是在 html 里你ng-repeat的原始数据: 4                   // start 也就是 paging 后面传的参数,即 currentPage*listsPerPage 5   return lists.slice(start);   //将原始数据按照 start 分割 6  }; 7 }); 8  9 demoApp.controller('demoCtrl',['$scope',function($scope){ //页面控制器10  $scope.demoLists = [                   //模拟数据11   {name:['hello world','hello world again',12  'why i say hello wrold',13  'i dont know the reason',14  'maybe because i am a developer.',15  'thank you for reading this',16  'why i say thank you',17  'cause this stuff has nothing to do with your angularJs studying',18  'these are just demo sentences.',19  'Do not have any special meanings.',20  'and you still take time to read this row by row',21  'what could i say?',22  'okay.maybe you wanna lenrn how json works.']23   }24 ];25   $scope.dataNum = $scope.demoLists[0].name.length; //获得数据总个数26   $scope.pages = Math.ceil($scope.dataNum/3);     //按照每页显示3个数据,得到总页数27   $scope.pageNum = [];                //生成页码,在 html里 ng-repeat 出来28   for(var i=0;i<$scope.pages;i++){29    $scope.pageNum.push(i);30   }31 32   $scope.currentPage = 0;            //设置当前页是 033   $scope.listsPerPage = 3;           //设置每页显示 3 个34 35   $scope.setPage = function(num){       // 当点击页码数字时执行的函数36    $scope.currentPage = num;         //将当前页 设置为 页码数37   }38 39   $scope.prevPage = function(){        //点击上一页执行的函数40      if($scope.currentPage > 0){41        $scope.currentPage--;42      }43    }44   $scope.nextPage = function(){       //点击下一页执行的函数45      if ($scope.currentPage < $scope.pages-1){46        $scope.currentPage++;47      }48    }49 }]);

 

这中间要说一下,你生成的 pageNum 是从 0 开始的,但真正的 页码 都是从一开始,所以这也就是 html 里 18 行是 num +1 的缘故。

页面效果 http://plnkr.co/edit/te45WhBd0yBaE4TekxFW?p=preview 

 

带省略号的下次写。什么你问为啥?鼓励你自己思考啊。成功就在你的不远处了。

用angularJs实现分页功能,不带省略号。

 




原标题:用angularJs实现分页功能,不带省略号。

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

马来西亚海运运费多少钱:https://www.goluckyvip.com/tag/92293.html
马来西亚海运费用价格:https://www.goluckyvip.com/tag/92294.html
马来西亚海运收费:https://www.goluckyvip.com/tag/92295.html
中国到马来西亚海运多少钱:https://www.goluckyvip.com/tag/92296.html
马来西亚海运多钱:https://www.goluckyvip.com/tag/92297.html
到马来西亚的海运价格:https://www.goluckyvip.com/tag/92298.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流