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做无刷新分页,这时候问题就来了,列表数据中有个轮播图用到 ...
随机推荐
- jQuery取得select选择的文本与值
jquery获取select选择的文本与值获取select :获取select 选中的 text :$("#ddlregtype").find("option:selec ...
- node爬虫之gbk网页中文乱码解决方案
之前在用 node 做爬虫时碰到的中文乱码问题一直没有解决,今天整理下备忘.(PS:网上一些解决方案都已经不行了) 中文乱码具体是指用 node 请求 gbk 编码的网页,无法正确获取网页中的中文(需 ...
- RapidJSON 代码剖析(一):混合任意类型的堆栈
大家好,这个专栏会分析 RapidJSON (中文使用手册)中一些有趣的 C++ 代码,希望对读者有所裨益. C++ 语法解说 我们先来看一行代码(document.h): bool StartArr ...
- Redis集群(四):主从配置二
一.本文目的 主要介绍redis主从模式下各种情况 二.说明 主从的基本概念:Master用于写入,Slaver用于读取,不能写入或修改,一个Master可以对应多个Slaver Mas ...
- 使用antd UI组件有感
公司使用的的react.js的版本提14.7的,JS版本使用的是ES6语法,因此在使用antd过程中,有些许不愉快的记录,分享给大家,一起学习: 如果是react 14.7版本时,使用getField ...
- 解决:error: .repo/manifests/: contains uncommitted changes
repo sync同步时提示出错: error: .repo/manifests/: contains uncommitted changes 解决方法: 1.cd 进入.repo/ ...
- cookie、sessionStorage、localStorage
转自--http://www.cnblogs.com/fly_dragon/p/3946012.html cookie Cookie的大小.格式.存储数据格式等限制,网站应用如果想在浏览器端存储用户的 ...
- 基于Twitter-Snowflake的java改进版,去状态化实现
package jeffery; import java.net.InetAddress; import java.net.UnknownHostException; import java.util ...
- Beta阶段第三次Scrum Meeting
情况简述 Beta阶段第三次Scrum Meeting 敏捷开发起始时间 2016/12/12 22:00 敏捷开发终止时间 2016/12/13 22:00 会议基本内容摘要 讨论决定了APP的名称 ...
- XmlSerializer 对象的Xml序列化和反序列化
http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html 这篇随笔对应的.Net命名空间是System.Xm ...