Angularjs实现分页和分页算法的示例代码

对于大多数web应用来说显示项目列表是一种很常见的任务。通常情况下,我们的数据会比较多,无法很好地显示在单个页面中。在这种情况下,我们需要把数据以页的方式来展示。

页面展示效果

页面HTML代码

rush:xhtml;"> 名称
  • disabled'}[p_current==1]">
  • disabled'}[p_current==p_all_page]">

Js代码

rush: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,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]

rush:js;"> var calculateIndexes = function (current,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; };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...