分享一个asp.net pager分页控件


Posted in Javascript onJanuary 04, 2012

效果:

分享一个asp.net pager分页控件
js:

$.fn.extend({ JPager: function (cfg, pageIndex, pageSize) { 
if (cfg && pageIndex > 0 && pageSize>0) { 
var token = "#" + this.attr("id"); 
this.empty(); 
var pageFirst = function () { 
$(token).JPager(cfg, 1, pageSize); 
}; 
var pagePre = function () { 
$(token).JPager(cfg, pageIndex - 1, pageSize); 
}; 
var pageLast = function () { 
$(token).JPager(cfg, parseInt($("#_tot").val()), pageSize); 
}; 
var pageNext = function () { 
$(token).JPager(cfg, pageIndex + 1, pageSize); 
}; 
var pageNumber = function () { 
$(token).JPager(cfg, parseInt($(this).text()), pageSize); 
}; 
var pageGo = function () { 
var index = parseInt($("#_pos").val()); 
var total = parseInt($("#_tot").val()); 
if (index) { 
if (index > total) { 
$(token).JPager(cfg, total, pageSize); 
} 
else if (index < 1) { 
$(token).JPager(cfg, 1, pageSize); 
} 
else { 
$(token).JPager(cfg, index, pageSize); 
} 
} 
}; 
var checkGoNumber = function () { 
if (!Number(this.value)) { 
this.value = ""; 
} 
else { 
this.value = Number(this.value); 
} 
}; 
var initCustomer = function (recordCount) { 
if (cfg.customer) { 
if (cfg.customer.template) { 
var t = cfg.customer.template; 
t = t.replace(/\%total\%/gi, Math.ceil(recordCount / pageSize)).replace(/\%current\%/gi, pageIndex).replace(/\%recordCount\%/gi, recordCount).replace(/\%pageSize\%/gi, pageSize); 
if (cfg.customer.position == "right") { 
$("#_right").after(t); 
} 
else { 
$("#_left").before(t); 
} 
} 
} 
}; 
var changeState = function (total) { 
if (pageIndex == 1) { 
$("#_first").attr("class", "unable"); 
$("#_pre").attr("class", "unable"); 
} 
else { 
$("#_first").bind("click", pageFirst).attr("class", "number"); 
$("#_pre").bind("click", pagePre).attr("class", "number"); 
} 
if (pageIndex == total) { 
$("#_last").attr("class", "unable"); 
$("#_next").attr("class", "unable"); 
} 
else { 
$("#_last").bind("click", pageLast).attr("class", "number"); 
$("#_next").bind("click", pageNext).attr("class", "number"); 
} 
}; 
var initNumber = function (total, count, current) { 
if (total > 0 && count > 0) { 
if (current < 1) { 
current = 1; 
} 
if (current > total) { 
current = total; 
} 
var endIndex = total; 
var startIndex = 1; 
var temp = current + Math.floor(count / 2); 
if (temp < total) { 
if (temp < count) { 
endIndex = count; 
} 
else { 
startIndex = temp - count + 1; 
endIndex = temp; 
} 
} 
else { 
if (total > count) { 
startIndex = total - count + 1; 
} 
} 
$("#_number").empty(); 
for (var i = startIndex; i <= endIndex; i++) { 
var html = $("<span></span>").text(i).bind("click", pageNumber); 
if (i == current) { 
$("#_number").append(html.attr("class", "selected")); 
} 
else { 
$("#_number").append(html.attr("class", "number")); 
} 
} 
} 
}; 
var initPager = function (data) { 
if ($.isArray(data.SearchResult) && data.RecordCount > 0) { 
$(token).append("<span id='_left'><span id='_first' class='spcial'>首页</span> <span id='_pre' class='spcial'>上一页</span></span> <span id='_number'></span><span id='_go'><input id='_pos' type='text'/><input id='_to' type='button' value='GO'/></span> <span id='_right'><span id='_next' class='spcial'>下一页</span> <span id='_last' class='spcial'>末页</span></span><input id='_tot' type='hidden'/>"); 
var total = Math.ceil(data.RecordCount / pageSize); 
$("#_tot").val(total); 
$("#_pos").bind("blur", checkGoNumber); 
$("#_to").bind("click", pageGo); 
changeState(total); 
if (cfg.showNumber && cfg.count > 0) { 
initNumber(total, cfg.count, pageIndex); 
} 
initCustomer(data.RecordCount); 
} 
}; 
if (cfg.action) { 
if (cfg.action.url && cfg.action.data) { 
var d = cfg.action.data.substr(0, cfg.action.data.lastIndexOf("}")) + ',"pageIndex":' + pageIndex + ',"pageSize":' + pageSize + "}"; 
if (cfg.action.callback && $.isFunction(cfg.action.callback)) { 
$.ajax({ 
type: "post", 
url: cfg.action.url, 
dataType: "json", 
contentType: "text/json", 
data: d, 
success: function (data) { 
initPager(data.d); 
cfg.action.callback(data.d); 
} 
}); 
} 
else { 
$.ajax({ 
type: "post", 
url: cfg.action.url, 
dataType: "json", 
contentType: "text/json", 
data: d, 
success: function (data) { 
initPager(data.d); 
} 
}); 
} 
} 
} 
} 
} 
});

css:
#_pos { 
width: 40px; 
} 
.unable 
{ 
color: #BCC0BB; 
} 
.number 
{ 
margin: 2px; 
color:#0000FF; 
text-decoration:underline; 
} 
.selected 
{ 
margin: 2px; 
color: #FF0000; 
font-weight: bold; 
}

html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>分页控件示例</title> 
<link href="CSS/JPager.css" rel="stylesheet" type="text/css" /> 
<script src="JS/jquery.min.js" type="text/javascript"></script> 
<script src="JExtension/JPager.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(function () { 
$("#pager").JPager({ customer:{template:"%cuRRent%"},count: 10, action: { url: "Service/JService.svc/GetPersons", data: '{"name":"zhoulq"}'}, showNumber: true },1,5); 
}); 
</script> 
</head> 
<body> 
<table> 
</table> 
<div id="pager"></div> 
</body> 
</html>

wcf:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
namespace JPlugin 
{ 
[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class JService 
{ 
[OperationContract] 
[WebInvoke] 
public PageObject<Person> GetPersons(string name,int pageIndex,int paseSize) 
{ 
return new PageObject<Person>(){RecordCount = 23,SearchResult = new List<Person>(){new Person(){Name="zhpulq",Age = 28},new Person(){Name = "zhouxy",Age = 24}}}; 
} 
} 
public class PageObject<T> 
{ 
public int RecordCount { get; set; } 
public List<T> SearchResult { get; set; } 
} 
}
Javascript 相关文章推荐
AJAX架构之Dojo篇
Apr 10 Javascript
解决iframe的frameborder在chrome/ff/ie下的差异
Aug 12 Javascript
javascript实现图片切换的幻灯片效果源代码
Dec 12 Javascript
Jquery chosen动态设置值实例介绍
Aug 08 Javascript
原生js封装的一些jquery方法(详解)
Sep 20 Javascript
微信小程序 页面跳转传参详解
Oct 28 Javascript
Angular.js去除页面中显示的空行方法示例
Mar 30 Javascript
js异步编程小技巧详解
Aug 14 Javascript
基于JavaScript实现五子棋游戏
Aug 26 Javascript
使用JS获取SessionStorage的值
Jan 12 Javascript
JS高阶函数原理与用法实例分析
Jan 15 Javascript
jQuery实现当拉动滚动条到底部加载数据的方法分析
Jan 24 jQuery
调试Node.JS的辅助工具(NodeWatcher)
Jan 04 #Javascript
DOM和XMLHttpRequest对象的属性和方法整理
Jan 04 #Javascript
40个有创意的jQuery图片和内容滑动及弹出插件收藏集之三
Jan 03 #Javascript
40个有创意的jQuery图片和内容滑动及弹出插件收藏集之二
Dec 31 #Javascript
40个有创意的jQuery图片、内容滑动及弹出插件收藏集之一
Dec 31 #Javascript
js 遍历对象的属性的代码
Dec 29 #Javascript
js当一个变量为函数时 应该注意的一点细节小结
Dec 29 #Javascript
You might like
php 短链接算法收集与分析
2011/12/30 PHP
PHP MYSQL实现登陆和模糊查询两大功能
2016/02/05 PHP
锋利的jQuery 第三章章节总结的例子
2010/03/23 Javascript
jquery 跳到顶部和底部动画2句代码简单实现
2013/07/18 Javascript
前台js对象在后台转化java对象的问题探讨
2013/12/20 Javascript
原生javascript实现简单的datagrid数据表格
2015/01/02 Javascript
JavaScript模拟实现继承的方法
2015/03/30 Javascript
javascript数字验证的实例代码(推荐)
2016/08/20 Javascript
AngularJs  unit-testing(单元测试)详解
2016/09/02 Javascript
详解Javascript中prototype属性(推荐)
2016/09/03 Javascript
jQuery插件form-validation-engine正则表达式操作示例
2017/02/09 Javascript
elemetUi 组件--el-upload实现上传Excel文件的实例
2017/10/27 Javascript
layer实现关闭弹出层刷新父界面功能详解
2017/11/15 Javascript
使用vue-infinite-scroll实现无限滚动效果
2018/06/22 Javascript
详解Nuxt.js 实战集锦
2019/11/19 Javascript
vue和小程序项目中使用iconfont的方法
2020/05/19 Javascript
[00:33]DOTA2上海特级锦标赛 CDEC战队宣传片
2016/03/04 DOTA
itchat接口使用示例
2017/10/23 Python
python 计算数组中每个数字出现多少次--“Bucket”桶的思想
2017/12/19 Python
从运行效率与开发效率比较Python和C++
2018/12/14 Python
Django上线部署之IIS的配置方法
2019/08/22 Python
解析PyCharm Python运行权限问题
2020/01/08 Python
深入浅析python的第三方库pandas
2020/02/13 Python
python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例
2020/03/01 Python
完美解决python针对hdfs上传和下载的问题
2020/06/05 Python
Python 程序员必须掌握的日志记录
2020/08/17 Python
德国奢侈品网上商城:Mytheresa
2016/08/24 全球购物
电子信息专业自荐书
2014/02/04 职场文书
毕业生求职信范文
2014/06/29 职场文书
舞出我人生观后感
2015/06/16 职场文书
篮球赛闭幕式主持词
2015/07/03 职场文书
小学运动会加油稿
2015/07/22 职场文书
2016年“9.22”世界无车日活动小结
2016/04/05 职场文书
个人房屋租赁合同(标准范本)
2019/09/16 职场文书
浅谈Python类的单继承相关知识
2021/05/12 Python
解决numpy和torch数据类型转化的问题
2021/05/23 Python