栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

AngularJs实现分页功能不带省略号的代码

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AngularJs实现分页功能不带省略号的代码

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

首先上 html 代码:






demo





  • {{sentences}}
上一页 {{num+}} 下一页

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

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

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

ul>li{
list-style:none;
width:px;
height:px;
border:px solid #CAF;
margin-bottom:px;
padding-left:px;
}
.nextlink,.prevlink{
font-size: px;
line-height: px;
height: px;
border: solid px #aaa;
color: #;
padding: px;
margin: px;
list-style: none;
background: #fff;
float: left;
cursor: pointer;
}
a.prevlink:hover,a.nextlink:hover {
background: #aaa !important;
color: #fff !important;
cursor: pointer;
}
.step {
display: block;
line-height: px;
height: px;
border: solid px #aaa;
color: #;
background: #fff;
padding: px;
font-size: px;
float: left;
margin: px;
list-style: none;
cursor: pointer;
}
.currentStep{
border-color: #fff;
padding: px;
color: #f;
font-weight: bold;
float: left;
display: block;
line-height: px;
height: px;
padding: px;
font-size: px;
float: left;
margin: px;
list-style: none;
cursor: pointer;
}

最后就是 js 了

var demoApp = angular.module('demoApp',[]);
demoApp.filter('paging',function(){ //paging 过滤器
return function(lists,start){ //两个参数 lists 是在 html 里你ng-repeat的原始数据:
// start 也就是 paging 后面传的参数,即 currentPage*listsPerPage
return lists.slice(start); //将原始数据按照 start 分割
};
});
demoApp.controller('demoCtrl',['$scope',function($scope){ //页面控制器
$scope.demoLists = [ //模拟数据
{name:['hello world','hello world again',
'why i say hello wrold',
'i dont know the reason',
'maybe because i am a developer.',
'thank you for reading this',
'why i say thank you',
'cause this stuff has nothing to do with your angularJs studying',
'these are just demo sentences.',
'Do not have any special meanings.',
'and you still take time to read this row by row',
'what could i say?',
'okay.maybe you wanna lenrn how json works.']
}
];
$scope.dataNum = $scope.demoLists[].name.length; //获得数据总个数
$scope.pages = Math.ceil($scope.dataNum/); //按照每页显示个数据,得到总页数
$scope.pageNum = []; //生成页码,在 html里 ng-repeat 出来
for(var i=;i<$scope.pages;i++){
$scope.pageNum.push(i);
}
$scope.currentPage = ; //设置当前页是 
$scope.listsPerPage = ; //设置每页显示 个
$scope.setPage = function(num){ // 当点击页码数字时执行的函数
$scope.currentPage = num; //将当前页 设置为 页码数
}
$scope.prevPage = function(){ //点击上一页执行的函数
if($scope.currentPage > ){
$scope.currentPage--;
}
}
$scope.nextPage = function(){ //点击下一页执行的函数
if ($scope.currentPage < $scope.pages-){
$scope.currentPage++;
}
}
}]);

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

以上内容是小编给大家介绍的AngularJs实现分页功能不带省略号的代码,希望能够帮助到大家,如果大家想了解更多有关angularjs的知识敬请关注考高分网网站!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/96607.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号