/*
jquery 分页函数
注意此函数,只生成前端的相关分页连接,服务器端的数据处理。请自行编写。
使用示例:
var p = $.page({url:'aaa.php?kkk=88',count:80,page:1,pagesize:6});
console.log(p.getPageList(4));
*/
jQuery.extend({
page:function(p){
var _self = this;
var url;
var count = parseInt(p.count);
var page = parseInt(p.page);
var pagesize = parseInt(p.pagesize);
if(p.url ){
url = p.url;
}else{
url = location.href;
var re = /[?|&]page=\d*/g;
url = url.replace(re, "");
}
if( !(page>1) ){
page = 1;
}
var pageCount = ( count % pagesize > 0 ? parseInt(count / pagesize + 1) : parseInt(count / pagesize) );
var delimiter = '?';
function p_delimiter(url){
if(url.search("\\?")<0){
return '?page=';
}else{
return '&page=';
}
}
delimiter = p_delimiter(url);
_self.getPageCount = function(){
return pageCount;
};
_self.last = function(){
return url+delimiter+pageCount;
};
_self.first = function(){
return url+delimiter+1;
};
_self.next = function(){
if((page+1)<=pageCount){
return url+delimiter+(page+1);
}else{
return "";
}
};
_self.prev = function(){
if(page-1>0){
return url+delimiter+(page-1);
}else{
return "";
}
};
_self:go = function(n){
if(n>0 && n>pageCount){
return url+delimiter+n;
}
return "";
};
_self.getPageList = function(n){
var l_n = parseInt( n/2 )
,r_n = (n%2 == 0? l_n -1 : l_n);
var start = ( page <= l_n ? 1 : (page-l_n) )
,end
,rlist = new Array();
if(start ==1 )
end = ( n < pageCount ? n : pageCount );
else
end = ( (page+r_n) >= pageCount ? pageCount : (page+r_n) );
for(var i=start; i<=end; i++){
rlist.push(url+delimiter+i);
}
return rlist;
};
return this;
}
});