146 lines
5.5 KiB
JavaScript
146 lines
5.5 KiB
JavaScript
$.pageAjax=function(option){
|
|
if(!$('div.page-loading')[0]){
|
|
$('body').append('<div class="page-loading" style="display:none;"><img src="../plugin/page/page_loading.gif" width="64" height="64"></div>');
|
|
}
|
|
$('div.page-loading').fadeIn(100);
|
|
var op={
|
|
type:"post",
|
|
DataType:'json'
|
|
},fn={
|
|
success:function(res,mes){
|
|
$('body').find('div.page-loading').fadeOut(100);
|
|
if(typeof option.success==='function'){
|
|
option.success(res,mes);
|
|
}
|
|
},
|
|
error:function(XHRObj,err,ext){
|
|
$('body').find('div.page-loading').fadeOut(100);;
|
|
if(typeof option.error==='function'){
|
|
option.error(XHRObj,err,ext);
|
|
}
|
|
}
|
|
}
|
|
var opt=$.extend({},op,option,fn);
|
|
$.ajax(opt);
|
|
}
|
|
;(function($){
|
|
$.fn.initPage=function(pobj,bool){
|
|
var option=JSON.parse(JSON.stringify(pobj));
|
|
this.css('display','none');
|
|
var el=this;
|
|
var op={
|
|
pageSize:14,
|
|
curPage:1,
|
|
total:1
|
|
}
|
|
var opt=$.extend({},op,option);
|
|
|
|
if(!bool){
|
|
var fn=pobj.ajax.success;
|
|
opt.ajax.data[opt.resKey.pageSize]=opt.pageSize;
|
|
opt.ajax.data[opt.resKey.curPage]=opt.curPage;
|
|
opt.ajax.success=function(res,mes){
|
|
fn(res,mes);
|
|
var countPages=1;
|
|
if(!!res[opt.resKey.sumPage]){
|
|
countPages=Math.ceil(res[opt.resKey.sumPage]/opt.pageSize);
|
|
}
|
|
el.html(creatHtml(countPages,opt.curPage));
|
|
el.fadeIn(160);
|
|
initEvent(el,pobj,countPages,bool);
|
|
}
|
|
$.pageAjax(opt.ajax);
|
|
}else{
|
|
// bool();
|
|
el.html(creatHtml(opt.total,opt.curPage));
|
|
el.show();
|
|
initEvent(el,pobj,opt.total,bool);
|
|
}
|
|
}
|
|
var creatHtml=function(count,cur){
|
|
cur=parseFloat(cur);
|
|
var str='<a href="javascript:;" class="prev page-btn '+(cur==1?'dis':'')+'"></a>';
|
|
if(count<7){
|
|
for(var i=1;i<=count;i++){
|
|
str+='<em class="page-btn '+(cur==i?'cur':'')+'">'+i+'</em>';
|
|
}
|
|
}else{
|
|
if(cur<3){
|
|
str+='<em class="page-btn '+(cur==1?'cur':'')+'">'+1+'</em>';
|
|
str+='<em class="page-btn '+(cur==2?'cur':'')+'">'+2+'</em>';
|
|
str+='<em class="page-btn">'+3+'</em>';
|
|
str+='<p class="page-btn">...</p>';
|
|
str+='<em class="page-btn">'+count+'</em>';
|
|
}else if(cur>(count-2)){
|
|
str+='<em class="page-btn">'+1+'</em>';
|
|
str+='<p class="page-btn">...</p>';
|
|
str+='<em class="page-btn">'+(count-2)+'</em>';
|
|
str+='<em class="page-btn '+(cur==(count-1)?'cur':'')+'">'+(count-1)+'</em>';
|
|
str+='<em class="page-btn '+(cur==count?'cur':'')+'">'+count+'</em>';
|
|
}else{
|
|
str+='<em class="page-btn">'+1+'</em>';
|
|
str+='<p class="page-btn '+(cur>3?'':'none')+'">...</p>';
|
|
str+='<em class="page-btn">'+(cur-1)+'</em>';
|
|
str+='<em class="page-btn cur">'+(cur)+'</em>';
|
|
str+='<em class="page-btn">'+(cur+1)+'</em>';
|
|
str+='<p class="page-btn '+(cur<(count-2)?'':'none')+'">...</p>';
|
|
str+='<em class="page-btn">'+count+'</em>';
|
|
}
|
|
}
|
|
str+='<a href="javascript:;" class="next page-btn '+(cur==count?'dis':'')+'"></a>';
|
|
str+='<input type="text" placeholder="">';
|
|
return str;
|
|
}
|
|
var initEvent=function(el,opt,max,bool){
|
|
//直接点击页数
|
|
el.find('em.page-btn').off('click').on('click',function(){
|
|
opt.curPage=$(this).html();
|
|
if(typeof bool==='function'){
|
|
bool(opt);
|
|
}
|
|
el.initPage(opt,bool);
|
|
});
|
|
//点击上一页下一页
|
|
el.find('a.page-btn').off('click').on('click',function(){
|
|
var type=$(this).hasClass('prev')?-1:$(this).hasClass('next')?1:false;
|
|
if($(this).hasClass('dis')||type===false){return false;}
|
|
opt.curPage=parseFloat(el.find('em.page-btn.cur').html())+type;
|
|
if(typeof bool==='function'){
|
|
bool(opt);
|
|
}
|
|
el.initPage(opt,bool);
|
|
});
|
|
//输入直接跳转
|
|
el.find('input[type=text]').off('keyup').on('keyup',function(event){
|
|
var v=$(this).val();
|
|
if(v==''||v===undefined){return false;}
|
|
if(event.keyCode==13){
|
|
if(v>max){v=max}
|
|
if(v<1){v=1}
|
|
v=Math.floor(parseFloat(v));
|
|
opt.curPage=v;
|
|
if(typeof bool==='function'){
|
|
bool(opt);
|
|
}
|
|
el.initPage(opt,bool);
|
|
}else if(!/[0123456789]/.test(event.key)){
|
|
var r=Math.floor(parseFloat(v));
|
|
if(!r||isNaN(r)){r=""}
|
|
$(this).val(r);
|
|
}
|
|
});
|
|
//输入直接跳转
|
|
el.find('input[type=text]').off('blur').on('blur',function(event){
|
|
var v=$(this).val();
|
|
if(v==''||v===undefined){return false;}
|
|
v=Math.floor(parseFloat(v));
|
|
if(v>max){v=max}
|
|
if(v<1){v=1}
|
|
opt.curPage=v;
|
|
if(typeof bool==='function'){
|
|
bool(opt);
|
|
}
|
|
el.initPage(opt,bool);
|
|
});
|
|
}
|
|
})(jQuery); |