152 lines
5.6 KiB
JavaScript
152 lines
5.6 KiB
JavaScript
|
|
$.pageAjax=function(option){
|
||
|
|
|
||
|
|
var op={
|
||
|
|
type:"post",
|
||
|
|
DataType:'json'
|
||
|
|
},fn={
|
||
|
|
beforeSend:function(){
|
||
|
|
var el=$('div.page-loading');
|
||
|
|
if(!el[0]){
|
||
|
|
$('body').append('<div class="page-loading" data-times="1" style="display:block;"><img src="../img/page_loading.gif" width="64" height="64"></div>');
|
||
|
|
}else{
|
||
|
|
showOrHide(el,'+');
|
||
|
|
}
|
||
|
|
el.fadeIn(100);
|
||
|
|
},
|
||
|
|
success:function(res,mes){
|
||
|
|
showOrHide($('body').find('div.page-loading'),'-');
|
||
|
|
if(typeof option.success==='function'){
|
||
|
|
option.success(res,mes);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
error:function(XHRObj,err,ext){
|
||
|
|
showOrHide($('body').find('div.page-loading'),'-');
|
||
|
|
if(typeof option.error==='function'){
|
||
|
|
option.error(XHRObj,err,ext);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var opt=$.extend({},op,option,fn);
|
||
|
|
$.ajax(opt);
|
||
|
|
}
|
||
|
|
function showOrHide(el,p){
|
||
|
|
var num=el.attr('data-times');
|
||
|
|
if(isNaN(num)){num=0;}
|
||
|
|
if(p=='+'){
|
||
|
|
el.attr('data-times',++num);
|
||
|
|
}else if(p=='-'){
|
||
|
|
el.attr('data-times',--num);
|
||
|
|
if(num<=0){
|
||
|
|
el.fadeOut(100);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
;(function($){
|
||
|
|
$.fn.initPage=function(pobj){
|
||
|
|
var option=JSON.parse(JSON.stringify(pobj));
|
||
|
|
this.css('display','none');
|
||
|
|
var op={
|
||
|
|
pageSize:14,
|
||
|
|
curPage:1
|
||
|
|
},fn=pobj.ajax.success,el=this;
|
||
|
|
var opt=$.extend({},op,option);
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
$.pageAjax(opt.ajax);
|
||
|
|
}
|
||
|
|
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>';
|
||
|
|
var isms=navigator.userAgent.toLowerCase();
|
||
|
|
if(isms.indexOf('msie 8.0')!==-1){
|
||
|
|
str+='<input type="text" value="跳转">';
|
||
|
|
}else{
|
||
|
|
str+='<input type="text" placeholder="跳转">';
|
||
|
|
}
|
||
|
|
return str;
|
||
|
|
}
|
||
|
|
var initEvent=function(el,opt,max){
|
||
|
|
//直接点击页数
|
||
|
|
el.find('em.page-btn').off('click').on('click',function(){
|
||
|
|
opt.curPage=$(this).html();
|
||
|
|
el.initPage(opt);
|
||
|
|
});
|
||
|
|
//点击上一页下一页
|
||
|
|
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;
|
||
|
|
el.initPage(opt);
|
||
|
|
});
|
||
|
|
//输入直接跳转
|
||
|
|
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;
|
||
|
|
el.initPage(opt);
|
||
|
|
}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;
|
||
|
|
el.initPage(opt);
|
||
|
|
});
|
||
|
|
el.find('input[type=text]').off('focus').on('focus',function(event){
|
||
|
|
var v=$(this).val();
|
||
|
|
if(isNaN(v)){
|
||
|
|
$(this).val('');
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
})(jQuery);
|