87,997
社区成员




/**
*
* @param {} recordCount 总的记录数
* @param {} pageSize 每页的记录数
* @param {} divId 容纳分页元素的id
* @param {} funPageSearch 回调的查询方法
* currentPage 当前页,默认为1
* @author:yxd
* @email:yxd_yxd2008@163.com
*/
function Pager(recordCount,pageSize,divId,funPageSearch,currentPage){
this.recordCount=recordCount;
this.pageSize=pageSize;
this.pageContainer=$("#"+divId);
this.funPageSearch=funPageSearch;
this.currentPage=currentPage || 1;
this.totalPage=this.calculateTotalPage(recordCount);
this.htmlRecordCount=null;
this.htmlTotalPage=null;
this.htmlInputPage=null;
this.htmlFirstPage=null;
this.htmlPrvPage=null;
this.htmlNextPage=null;
this.htmlLastPage=null;
this.htmlDiv=null;
this.draw();
}
Pager.prototype={
draw:function(){
var oo=this;
var str='共<span>'+this.recordCount+'</span>行 '+
'每页'+this.pageSize+'行 '+
'共<span>'+this.totalPage+'</span>页 '+
'<div style="display:none;">转到第<input value="'+this.currentPage+'" style="width:28px" maxlength="'+this.maxLength()+'">页 '+
'<span>首页</span> '+
'<span>上页</span> '+
'<span>下页</span> '+
'<span>尾页</span></div> ';
this.pageContainer.append(str);
this.htmlRecordCount=$("span:eq(0)",this.pageContainer);
this.htmlTotalPage=$("span:eq(1)",this.pageContainer);
this.htmlInputPage=$("input:eq(0)",this.pageContainer);
this.htmlFirstPage=$("span:eq(2)",this.pageContainer);
this.htmlPrvPage=$("span:eq(3)",this.pageContainer);
this.htmlNextPage=$("span:eq(4)",this.pageContainer);
this.htmlLastPage=$("span:eq(5)",this.pageContainer);
this.htmlDiv=$("div",this.pageContainer);
if(this.totalPage>1){
this.htmlDiv.css("display","inline");
}
//设定按钮的样式
this.setButtonStyle();
//为按钮绑定事件
//首页
this.htmlFirstPage.click(function(){
if(oo.currentPage!=1){
oo.currentPage=1;
oo.gotoPage();
}
});
//上页
this.htmlPrvPage.click(function(){
if(oo.currentPage!=1){
oo.currentPage--;
oo.gotoPage();
}
});
//下页
this.htmlNextPage.click(function(){
if(oo.currentPage!=oo.totalPage){
oo.currentPage++;
oo.gotoPage();
}
});
//尾页
this.htmlLastPage.click(function(){
if(oo.currentPage!=oo.totalPage){
oo.currentPage=oo.totalPage;
oo.gotoPage();
}
});
//处理输入框的事件
this.htmlInputPage.focus(function(){
this.select();
}).change(function(){
if( isNaN(Number(this.value))
|| Number(this.value)==0
|| Number(this.value)>oo.totalPage){
this.value=oo.currentPage;
return;
}
oo.currentPage=Number(this.value);
oo.gotoPage();
}).keydown(function(evt){
var validKey={48:0,49:1,50:2,51:3,52:4,53:5,54:6,55:7,56:8,57:9,
96:0,97:1,98:2,99:3,100:4,101:5,102:6,103:7,104:8,105:9,
8:'',37:'',39:'',46:''};
if(!(evt.keyCode in validKey)){
return false;
}
});
},
gotoPage:function(){
if(this.htmlInputPage.val()!=this.currentPage){
this.htmlInputPage.val(this.currentPage);
}
this.setButtonStyle();
this.funPageSearch(this.currentPage);
},
maxLength:function(){//计算输入页数的长度
return (this.totalPage+'').length;
},
setButtonStyle:function(){//根据当前的页数设置按钮的样式
switch(this.currentPage){
case 1:{
this.htmlFirstPage.css({cursor:"",color:"grey"});
this.htmlPrvPage.css({cursor:"",color:"grey"});
this.htmlNextPage.css({cursor:"pointer",color:"black"});
this.htmlLastPage.css({cursor:"pointer",color:"black"});
break;
}
case this.totalPage:{
this.htmlFirstPage.css({cursor:"pointer",color:"black"});
this.htmlPrvPage.css({cursor:"pointer",color:"black"});
this.htmlNextPage.css({cursor:"",color:"grey"});
this.htmlLastPage.css({cursor:"",color:"grey"});
break;
}
default:{
this.htmlFirstPage.css({cursor:"pointer",color:"black"});
this.htmlPrvPage.css({cursor:"pointer",color:"black"});
this.htmlNextPage.css({cursor:"pointer",color:"black"});
this.htmlLastPage.css({cursor:"pointer",color:"black"});
break;
}
}
},
calculateTotalPage:function(_recordCount){//计算出总页数
return Math.floor((_recordCount+this.pageSize-1)/this.pageSize);
},
reDraw:function(_recordCount){//对于不刷新的查询,需要根据总记录数来重新设置分页元素
this.currentPage=1;
this.recordCount=Number(_recordCount);
this.totalPage=this.calculateTotalPage(this.recordCount);
if(this.totalPage<2){
this.htmlDiv.css("display","none");
}else{
this.htmlDiv.css("display","inline");
}
this.setButtonStyle();
this.htmlRecordCount.html(this.recordCount);
this.htmlTotalPage.html(this.totalPage);
this.htmlInputPage.val(this.currentPage);
this.htmlInputPage.attr("maxlength",this.maxLength());
},
constructor:Pager
}
function fy(L,l,All,s_r,page,tj,id)//L分页长度,l是每页数量,All是总量,s_r是输入的页数,后面的可有可无,比较粗糙还没优化好
{
s_r=Math.floor(s_r);
var min = 0;
var max = 0;
var D = 0;
var pagecount = (All % l == 0) ? (All / l) : (Math.floor(All / l) + 1);//页数
var L_C =(L % 2 == 0) ? (L / 2) : (Math.floor(L / 2) + 1);//页码居中
if (s_r <= 0 || s_r > pagecount)//第一种情况:输入数不在有效的页数内,所以把它定位到第一页
{
min = 1;
D = 1;
max = (pagecount > L) ? L : pagecount;
}
else
{
if (pagecount <= L)//第二种情况:当总页数小于页码长度时
{
min = 1;
D = s_r;
max = pagecount;
}
else
{
if (s_r <= L_C)//第三种情况:
{
min = 1;
D = s_r;
max = L;
}
else
{
if ((pagecount - s_r)<= L_C)//第四种情况:
{
min = pagecount - L + 1;
D = s_r;
max = pagecount;
}
else
{
min = s_r - L_C + 1;
D = s_r;
max =(L % 2 == 0) ? (s_r + L_C) : (s_r + L_C - 1);
//alert(s_r +"___"+ L_C +"____"+ 1)
//alert(min+"____"+max+"____"+D+"____"+s_r+"____"+L_C+"____"+L+"____"+pagecount)
}
}
}
}
var a = "";
if (tj != null)
{
for(var c in tj)
{
a += "&" + tj[c];
if(c==tj.length-1)
{
break;
}
}
}
var fy = "";
for (var kg = min; kg <= max; kg++)
{
//alert(D);
fy += (kg == D) ? "<span class=box>" + kg + "</span> " : " <span class=box2 onclick=\"send_to_search("+kg+","+id+")\" >" + kg + "</span> ";
}
var FY = "<span class=box2 onclick=\"send_to_search("+1+","+id+")\" >首页</span> <span class=box2 onclick=\"send_to_search("+(D == 1 ? 1 : (D - 1))+","+id+")\" >上一页</span> " + fy + " <span class=box2 onclick=\"send_to_search("+((D < max) ? (D == 1 ? 2 : (D + 1)) : ((max == 0 ? max + 1 : max)))+","+id+")\" >下一页</span> <span class=box2 onclick=\"send_to_search("+pagecount+","+id+")\" >尾页</span> <span>定位到第<input type=\"text\" class=\"box2\" value=\""+kg+"\" style=\"width:32px; height:20px;margin: 0px 0px 0px 0px;padding:0px 0px 0px 0px;text-align:center;\" />页</span><span class=box2 onclick=\"send_to_search("+pagecount+","+id+")\" >点击定位</span>";
//maxp=pagecount;
return FY;
}