AngularJS 分页
前端源码:
<div>
<h1>列表页33</h1>
<table>
<thead>
<tr><td>CandiID</td><td>HotelName</td></tr>
</thead>
<tr ng-repeat="item in List track by $index"><td>{{item.CandiID}}</td><td>{{item.HotelName}}</td></tr>
</table>
<div>
<%--<pagination ></pagination>--%>
</div>
<div>
<tm-pagination conf="paginationConf"></tm-pagination>
</div>
</div>
AngularJS
var listModule = angular.module('app.farmhouselist', []);
//控制器
var listCtrl = function ($scope, $http, $q, GetCandidateListByCondition) {
var conditionObj = new Object();
conditionObj.Province = 23;
conditionObj.SourceType = 128;
conditionObj.SalesManName = 'xixi';
conditionObj.UserCategory = "0";
conditionObj.CurrentUser = "xixi";
var condition = JSON.stringify(conditionObj);
var GetAllEmployee = function () {
var postData = {
pageIndex: $scope.paginationConf.currentPage,
pageSize: $scope.paginationConf.itemsPerPage
};
//任务单
GetCandidateListByCondition.event(condition, postData.pageIndex, postData.pageSize).then(function (data) {
$scope.List = eval(data.PageData);
$scope.totalCount = data.TotalCount;
$scope.paginationConf.totalItems = data.TotalCount;
});
}
//配置分页基本参数
$scope.paginationConf = {
currentPage: 1,
itemsPerPage: 15
};
$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetAllEmployee);
}
listModule.controller("listCtrl", ['$scope', listCtrl]);
//分页指令
listModule.directive("tmPagination", function () {
return {
restrict: 'EA',
//template: '<div class="page-list">' +
// '<ul class="pagination" ng-show="conf.totalItems > 0">' +
// '<li ng-class="{disabled: conf.currentPage == 1}" ng-click="prevPage()"><span>«</span></li>' +
// '<li ng-repeat="item in pageList track by $index" ng-class="{active: item == conf.currentPage, separate: item == \'...\'}" ' +
// 'ng-click="changeCurrentPage(item)">' +
// '<span>{{ item }}</span>' +
// '</li>' +
// '<li ng-class="{disabled: conf.currentPage == conf.numberOfPages}" ng-click="nextPage()"><span>»</span></li>' +
// '</ul>' +
// '<div class="page-total" ng-show="conf.totalItems > 0">' +
// '第 <input type="text" ng-model="jumpPageNum" ng-keyup="jumpToPage($event)"/> 页 ' +
// '每页 <select ng-model="conf.itemsPerPage" ng-options="option for option in conf.perPageOptions " ng-change="changeItemsPerPage()"></select>' +
// ' 共<strong>{{ conf.totalItems }}</strong> 条' +
// '</div>' +
// '<div class="no-items" ng-show="conf.totalItems <= 0">暂无数据</div>' +
// '</div>',
template: '<div class="page-list">' +
'<ul class="pagination" ng-show="conf.totalItems > 0">' +
'<li ng-class="{disabled: conf.currentPage == 1}" ng-click="prevPage()"><span>«</span></li>' +
'<li ng-repeat="item in pageList track by $index" ng-class="{active: item == conf.currentPage, separate: item == \'...\'}" ' +
'ng-click="changeCurrentPage(item)">' +
'<span>{{ item }}</span>' +
'</li>' +
'<li ng-class="{disabled: conf.currentPage == conf.numberOfPages}" ng-click="nextPage()"><span>»</span></li>' +
'</ul>' +
'<div class="page-total" ng-show="conf.totalItems > 0">' +
'第 <input type="text" ng-model="jumpPageNum" ng-keyup="jumpToPage($event)"/> 页 ' +
' 每页{{conf.itemsPerPage}}条' +
' 共<strong>{{ conf.totalItems }}</strong> 条' +
'</div>' +
'<div class="no-items" ng-show="conf.totalItems <= 0">暂无数据</div>' +
'</div>',
replace: true,
scope: {
conf: '='
},
link: function (scope, element, attrs) {
// 变更当前页
scope.changeCurrentPage = function (item) {
if (item == '...') {
return;
} else {
scope.conf.currentPage = item;
}
};
// 定义分页的长度必须为奇数 (default:9)
scope.conf.pagesLength = parseInt(scope.conf.pagesLength) ? parseInt(scope.conf.pagesLength) : 10;
if (scope.conf.pagesLength % 2 === 0) {
// 如果不是奇数的时候处理一下
scope.conf.pagesLength = scope.conf.pagesLength - 1;
}
// conf.erPageOptions
if (!scope.conf.perPageOptions) {
scope.conf.perPageOptions = [10, 15, 20, 30, 50];
}
// pageList数组
function getPagination() {
// conf.currentPage
scope.conf.currentPage = parseInt(scope.conf.currentPage) ? parseInt(scope.conf.currentPage) : 1;
//scope.conf.totalItems
//判断一下scope.conf.totalItems,如果是undefined,直接返回不执行后面的代码 add by cpf
if (scope.conf.totalItems==undefined) {
return;
}
scope.conf.totalItems = parseInt(scope.conf.totalItems);
// conf.itemsPerPage (default:15)
// 先判断一下本地存储中有没有这个值
if (scope.conf.rememberPerPage) {
if (!parseInt(localStorage[scope.conf.rememberPerPage])) {
localStorage[scope.conf.rememberPerPage] = parseInt(scope.conf.itemsPerPage) ? parseInt(scope.conf.itemsPerPage) : 15;
}
scope.conf.itemsPerPage = parseInt(localStorage[scope.conf.rememberPerPage]);
} else {
scope.conf.itemsPerPage = parseInt(scope.conf.itemsPerPage) ? parseInt(scope.conf.itemsPerPage) : 15;
}
// numberOfPages
scope.conf.numberOfPages = Math.ceil(scope.conf.totalItems / scope.conf.itemsPerPage);
// judge currentPage > scope.numberOfPages
if (scope.conf.currentPage < 1) {
scope.conf.currentPage = 1;
}
if (scope.conf.currentPage > scope.conf.numberOfPages) {
scope.conf.currentPage = scope.conf.numberOfPages;
}
// jumpPageNum
scope.jumpPageNum = scope.conf.currentPage;
// 如果itemsPerPage在不在perPageOptions数组中,就把itemsPerPage加入这个数组中
var perPageOptionsLength = scope.conf.perPageOptions.length;
// 定义状态
var perPageOptionsStatus;
for (var i = 0; i < perPageOptionsLength; i++) {
if (scope.conf.perPageOptions[i] == scope.conf.itemsPerPage) {
perPageOptionsStatus = true;
}
}
// 如果itemsPerPage在不在perPageOptions数组中,就把itemsPerPage加入这个数组中
if (!perPageOptionsStatus) {
scope.conf.perPageOptions.push(scope.conf.itemsPerPage);
}
// 对选项进行sort
scope.conf.perPageOptions.sort(function (a, b) { return a - b });
scope.pageList = [];
if (scope.conf.numberOfPages <= scope.conf.pagesLength) {
// 判断总页数如果小于等于分页的长度,若小于则直接显示
for (i = 1; i <= scope.conf.numberOfPages; i++) {
scope.pageList.push(i);
}
} else {
// 总页数大于分页长度(此时分为三种情况:1.左边没有...2.右边没有...3.左右都有...)
// 计算中心偏移量
var offset = (scope.conf.pagesLength - 1) / 2;
if (scope.conf.currentPage <= offset) {
// 左边没有...
for (i = 1; i <= offset + 1; i++) {
scope.pageList.push(i);
}
scope.pageList.push('...');
scope.pageList.push(scope.conf.numberOfPages);
} else if (scope.conf.currentPage > scope.conf.numberOfPages - offset) {
scope.pageList.push(1);
scope.pageList.push('...');
for (i = offset + 1; i >= 1; i--) {
scope.pageList.push(scope.conf.numberOfPages - i);
}
scope.pageList.push(scope.conf.numberOfPages);
} else {
// 最后一种情况,两边都有...
scope.pageList.push(1);
scope.pageList.push('...');
for (i = Math.ceil(offset / 2) ; i >= 1; i--) {
scope.pageList.push(scope.conf.currentPage - i);
}
scope.pageList.push(scope.conf.currentPage);
for (i = 1; i <= offset / 2; i++) {
scope.pageList.push(scope.conf.currentPage + i);
}
scope.pageList.push('...');
scope.pageList.push(scope.conf.numberOfPages);
}
}
if (scope.conf.onChange) {
scope.conf.onChange();
}
scope.$parent.conf = scope.conf;
}
// prevPage
scope.prevPage = function () {
if (scope.conf.currentPage > 1) {
scope.conf.currentPage -= 1;
}
};
// nextPage
scope.nextPage = function () {
if (scope.conf.currentPage < scope.conf.numberOfPages) {
scope.conf.currentPage += 1;
}
};
// 跳转页
scope.jumpToPage = function () {
scope.jumpPageNum = scope.jumpPageNum.replace(/[^0-9]/g, '');
if (scope.jumpPageNum !== '') {
scope.conf.currentPage = scope.jumpPageNum;
}
};
// 修改每页显示的条数
scope.changeItemsPerPage = function () {
// 清除本地存储的值方便重新设置
if (scope.conf.rememberPerPage) {
localStorage.removeItem(scope.conf.rememberPerPage);
}
};
scope.$watch(function () {
var newValue = scope.conf.currentPage + ' ' + scope.conf.totalItems + ' ';
// 如果直接watch perPage变化的时候,因为记住功能的原因,所以一开始可能调用两次。
//所以用了如下方式处理
if (scope.conf.rememberPerPage) {
// 由于记住的时候需要特别处理一下,不然可能造成反复请求
// 之所以不监控localStorage[scope.conf.rememberPerPage]是因为在删除的时候会undefind
// 然后又一次请求
if (localStorage[scope.conf.rememberPerPage]) {
newValue += localStorage[scope.conf.rememberPerPage];
} else {
newValue += scope.conf.itemsPerPage;
}
} else {
newValue += scope.conf.itemsPerPage;
}
return newValue;
}, getPagination);
}
};
});
AngularJS 分页的更多相关文章
- 自定义angularjs分页控件
继昨天写了knockoutjs+ jquery pagination+asp.net web Api 实现无刷新列表页 ,正好最近刚学习angularjs ,故琢磨着写一个angularjs版本的分页 ...
- angularjs 分页精华代码
//pageinfo $scope.pageSize=10;$scope.currentType={{ current_type }};$scope.currentPage={{ json_encod ...
- AngularJS分页实现
基本思路 一开始页码为1,Service向服务器端获取对应信息:点击上/下一页/跳转,通过对应的页码向服务器端获取对应的信息. 由于后台暂时没弄好,我实现的过程中直接读取准备好的JSON文件,通过页码 ...
- Angularjs 分页控件
实现效果: 实现步骤: 1.分页页面:page.html,页面多余样式,需要自己去除. <div class="row" ng-show="conf.totalIt ...
- angularjs分页组件
这是我第一次写博客,激动,首先,我也是个菜鸟,分享一下自己写的服务器端分页的代码,自己一步一步写的,其中也有参考别人的代码.技术比较渣,先这样了. // ====== 2019-1-3 ======/ ...
- 前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤
在上一篇中实现了增删改查,本篇实现分页和过滤. 本系列包括: 1.前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查2.前端使用AngularJS的$re ...
- Angularjs 跳转页面并传递参数的方法总结
http://www.zhihu.com/question/33565135 http://www.codeproject.com/Articles/1073780/ASP-NET-MVC-CRUD- ...
- angular.js分页代码的实例
对于大多数web应用来说显示项目列表是一种很常见的任务.通常情况下,我们的数据会比较多,无法很好地显示在单个页面中.在这种情况下,我们需要把数据以页的方式来展示,同时带有转到上一页和下一页的功能.现在 ...
- 有了jsRender,妈妈再也不用担心我用jq拼接DOM拼接的一团糟了、页面整齐了、其他伙伴读代码也不那么费劲了
写在前面 说来也很巧, 下午再做一个页面,再普通不过的分页列表,我还是像往常一样,基于MVC环境下,我正常用PagedList.MVC AJAX做无刷新分页,这时候问题就来了,列表数据中有个轮播图用到 ...
随机推荐
- Win10 UWP 开发学习代码(不断更新)
页面之间跳转(传值) string txt = "Spring Lee"; this.Frame.Navigate(typeof(BlankPage1),txt); 另一个页面接收 ...
- mysql CREATE USER
CREATE USER 'Rhine'@'%' IDENTIFIED BY 'Rhine@2015'; GRANT ALL ON *.* TO 'Rhine'@'%'; flush privilege ...
- 【Python】[面向对象高级编程] 多成继承,定制类,使用枚举
1.多成继承 class SmallDog(Animal,Dog) pass MixIn就是一种常见的设计. 2.定制类类似__slots__这种形如 __xxx__ 的变量或者函数名,在python ...
- [Bundling and Minification ] 三、缩小
Minification 缩小,将文档的Size减小.主要是通过一下方式缩小文档的Size: 1.移除没用的空行 . 2.删除代码注释. 3.缩短js变量的名称,将变量名用一个字母表示.
- 对于angularJS的一点思考
已经找好工作近两周了,入职基本上还算顺利,自己两年来的挑灯夜战也算是有了收获,于是这两周基本上是按部就班的工作,没有学习什么新技术.在上个公司的时候,同事在项目中使用angularJs,之前他也没有接 ...
- Eclipse Maven3新建web项目
环境: Eclipse Neon JDK1.8 先决条件: 本机下载apache-tomcat-8,解压,在Eclipse->preferences->server里配置安装目录.并在ec ...
- 使用MicroService4Net 快速创建一个简单的微服务
“微服务架构(Microservice Architecture)”一词在过去几年里广泛的传播,它用于描述一种设计应用程序的特别方式,作为一套独立可部署的服务.目前,这种架构方式还没有准确的定义,但是 ...
- es_Linux
命令:echo 显示.印出 echo $PATH 通过 "su - vbird"" 这个指令来变换到 vbird身份 权限: rwx rwx rwx 以4 2 1数字表 ...
- [NHibernate]基本配置与测试
目录 写在前面 nhibernate文档 搭建项目 映射文件 持久化类 辅助类 数据库设计与连接配置 测试 总结 写在前面 一年前刚来这家公司,发现项目中使用的ORM是Nhibernate,这个之前确 ...
- [NHibernate]N+1 Select查询问题分析
目录 写在前面 文档与系列文章 N+1 Select查询问题分析 总结 写在前面 在前面的文章(延迟加载,立即加载)中都提到了N+1 Select的问题,总觉得理解的很不到位,也请大家原谅,这也是为什 ...