对于大多数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"+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;
};
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Angularjs实现分页和分页算法的示例代码”评论...
更新日志
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
