Angularjs实现分页和分页算法

12 篇文章 0 订阅

页面展示效果:

页面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;
	};

 

 

分页算法是关键哦!!刚开始接触Angularjs太高深也不太懂!还请多多担待啦o(∩_∩)o 哈哈

感谢您的支持,写的文章如对您有所帮助,开源不易,请您打赏,谢谢啦~

原创:http://blog.csdn.net/qilin001cs

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
AngularJS实现分页通常需要使用到 ng-repeat 指令和自定义过滤器,以下是一个示例代码: HTML: ``` <div ng-app="myApp" ng-controller="myCtrl"> <ul> <li ng-repeat="item in filteredItems = (items | startFrom:(currentPage-1)*pageSize | limitTo:pageSize)">{{item}}</li> </ul> <div> <button ng-disabled="currentPage == 1" ng-click="currentPage=1">First</button> <button ng-disabled="currentPage == 1" ng-click="currentPage=currentPage-1">Previous</button> <button ng-disabled="currentPage == totalPages" ng-click="currentPage=currentPage+1">Next</button> <button ng-disabled="currentPage == totalPages" ng-click="currentPage=totalPages">Last</button> </div> <div> Current Page: {{currentPage}} </div> </div> ``` JavaScript: ``` var app = angular.module('myApp', []); app.filter('startFrom', function() { return function(input, start) { start = +start; return input.slice(start); } }); app.controller('myCtrl', function($scope) { $scope.items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10', 'item11', 'item12', 'item13', 'item14', 'item15']; $scope.currentPage = 1; $scope.pageSize = 5; $scope.totalPages = Math.ceil($scope.items.length / $scope.pageSize); }); ``` 在上面的代码中,我们定义了一个自定义过滤器 startFrom,它接收一个起始位置参数 start,返回从该位置开始的数组切片。我们还定义了一个控制器 myCtrl,其中 $scope.items 是我们需要分页的数组,$scope.currentPage 是当前页数,$scope.pageSize 是每页显示的数据量,$scope.totalPages 是总页数。在 HTML 中,我们使用 ng-repeat 指令和自定义过滤器来实现分页效果,并通过按钮来控制页码的变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值