码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-05-30 11:23:15      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

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="angular.min.js"></script>  <!-- 引入你的 angularJs 文件 -->
23   <script src="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;                       //设置当前页是 0
33     $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 的缘故。

 

 

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

技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/lijiayi/p/ngpaging1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!