-
Notifications
You must be signed in to change notification settings - Fork 28
/
b.page.min.js
1 lines (1 loc) · 7.63 KB
/
b.page.min.js
1
!function($){"use strict";var defaults={"pageSize":10,"pageNumber":1,"totalRow":0,"pageBarSize":5,"pageSizeMenu":[10,20,50,100],"params":undefined,"className":undefined,"asyncLoad":false,"asyncTarget":'body',"asyncType":'POST',"serverSidePage":false,"render":undefined,"url":'',"callback":$.noop};var template='<div class="pagination bPage bPageRight">'+'<ul>'+'<li id="bPageList" class="disabled bPageList"><a>每页 <select id="bPageDropList"></select> 条</a></li>'+'<li id="bPageInfo" class="disabled bPageInfo"><a> </a></li>'+'<li id="bPageFirstPage" class="bPageControlButton"><a href="javascript:void(0);">首页</a></li>'+'<li id="bPagePreviousPage" class="bPageControlButton"><a href="javascript:void(0);">«</a></li>'+'<li id="bPageNextPage" class="bPageControlButton"><a href="javascript:void(0);">»</a></li>'+'<li id="bPageLastPage" class="bPageControlButton"><a href="javascript:void(0);">尾页</a></li>'+'</ul>'+'</div>';var bPage=function(element,p){this.$container=element;this.p=p;this.pageNumber=Number(p.pageNumber);this.pageSize=Number(p.pageSize);this.totalRow=Number(p.totalRow);};bPage.version='1.1';bPage.eventName='click.bPage';bPage.pageInfoBoxClassName='paginationInfoBox';bPage.hiddenPageNumberId='bPagePageNumber';bPage.hiddenPageSizeId='bPagePageSize';bPage.hiddenTotalRowId='bPageTotalRow';bPage.prototype.init=function(){var self=this,p=this.p,elem=this.$container;var htmlstr=template;$(elem).append(htmlstr);if(p.className)$('div.bPage',elem).addClass(p.className);if(p.pageSizeMenu&&$.isArray(p.pageSizeMenu)&&p.pageSizeMenu.length>0){$.each(p.pageSizeMenu,function(i,row){if(row==this.pageSize){$('#bPageDropList',$(elem)).append('<option selected="selected">'+row+'</option>');}else{$('#bPageDropList',$(elem)).append('<option>'+row+'</option>');}});$('#bPageDropList',$(elem)).val(self.pageSize);}if(!p.asyncLoad)this.populate();this.eventBind();if(p.asyncLoad)this.pageSwitch(1);};bPage.prototype.updatePageInfo=function(){if($.type(this.pageNumber)=='undefined'||$.type(this.pageNumber)!='number'||this.pageNumber<=0)this.pageNumber=defaults.pageNumber;if($.type(this.pageSize)=='undefined'||$.type(this.pageSize)!='number'||this.pageSize<=0)this.pageSize=defaults.pageSize;if($.type(this.totalRow)!='undefined'&&$.type(this.totalRow)=='number')this.totalPage=Math.ceil(this.totalRow/this.pageSize);};bPage.prototype.populate=function(){this.updatePageInfo();var self=this,elem=this.$container,p=this.p,pNum=this.pageNumber;var _class,_start,_end,_half=Math.floor(p.pageBarSize/2);var pageInfo='当前显示第'+this.pageNumber+'/'+this.totalPage+' 页'+'(共'+this.totalRow+'条记录)';$('#bPageInfo a',$(elem)).html(pageInfo);if(this.totalPage<p.pageBarSize){_start=1;_end=this.totalPage;}else if(pNum<=_half){_start=1;_end=p.pageBarSize;}else if(pNum>=(this.totalPage-_half)){_start=this.totalPage-p.pageBarSize+1;_end=this.totalPage;}else{_start=pNum-_half;_end=_start+p.pageBarSize-1;}$('li:not(.bPageList,.bPageInfo,.bPageControlButton)',$(elem)).remove();for(var i=_start;i<=_end;i++){_class=(i==pNum)?'class="active"':'';var curPage=$('<li '+_class+'><a href="javascript:void(0);">'+i+'</a></li>').insertBefore($('#bPageNextPage',$(elem)));if(i!=pNum)this.setFunction($(curPage),i);}var _fNum,_pNum,_nNum,_lNum;if(pNum===1){$('#bPageFirstPage,#bPagePreviousPage',$(elem)).addClass('disabled');_fNum=-1;_pNum=-1;}else{$('#bPageFirstPage,#bPagePreviousPage',$(elem)).removeClass('disabled');_fNum=1;_pNum=pNum>1?pNum-1:1;}this.setFunction($('#bPageFirstPage a',$(elem)),_fNum);this.setFunction($('#bPagePreviousPage a',$(elem)),_pNum);if(pNum===this.totalPage||this.totalPage===0){$('#bPageNextPage,#bPageLastPage',$(elem)).addClass('disabled');_nNum=-1;_lNum=-1;}else{$('#bPageNextPage,#bPageLastPage',$(elem)).removeClass('disabled');_nNum=pNum<this.totalPage?pNum+1:this.totalPage;_lNum=this.totalPage;}this.setFunction($('#bPageNextPage a',$(elem)),_nNum);this.setFunction($('#bPageLastPage a',$(elem)),_lNum);};bPage.prototype.eventBind=function(){var self=this;$('#bPageDropList',$(this.$container)).on('change',function(event){var pSize=$(this).val();if(pSize)self.pageSize=Number(pSize);self.pageSwitch(1);});};bPage.prototype.buildParamsStr=function(){var str='',p=this.p;if(p.params&&$.isFunction(p.params)){var pa=p.params(),attr;if($.isPlainObject(pa)){for(attr in pa){str+='&'+attr+'='+pa[attr];}}}return str;};bPage.prototype.getParams=function(pageNumber){var param={},p=this.p;param.pageNumber=pageNumber;param.pageSize=this.pageSize;if(p.params&&$.isFunction(p.params)){var pa=p.params();if($.isPlainObject(pa)&&!$.isEmptyObject(pa))param=$.extend({},param,pa);}return param;};bPage.prototype.setFunction=function(obj,pageNumber){var self=this;$(obj).off(bPage.eventName).on(bPage.eventName,function(event){self.pageSwitch(pageNumber);});};bPage.prototype.extractPageInfo=function(data){if(!data)return;var done=true;var errorMsg='提取分页信息失败,请检查分页信息栏是否设置或指定的样式/ID设置有误';var box=undefined;$(data).each(function(i,row){if($(this).hasClass(bPage.pageInfoBoxClassName)){box=this;return false;}});var pNum=$('#'+bPage.hiddenPageNumberId,$(box)).val();var pSize=$('#'+bPage.hiddenPageSizeId,box).val();var tRow=$('#'+bPage.hiddenTotalRowId,box).val();if(!pNum||!pSize||!tRow){done=false;console.error(errorMsg);}else{try{if(pNum)this.pageNumber=Number(pNum);if(pSize)this.pageSize=Number(pSize);if(tRow)this.totalRow=Number(tRow);}catch(e){done=false;console.error(errorMsg);}}return done;};bPage.prototype.pageSwitch=function(pageNumber){var self=this,p=this.p;if(typeof(pageNumer)==undefined&&typeof(pageNumber)!='number')pageNumber=self.pageNumber;if(pageNumber==-1)return;if(pageNumber>self.totalPage)pageNumber=self.totalPage;if(p.asyncLoad){self.pageNumber=pageNumber;var param=self.getParams(pageNumber);var async=true;if(p.async!==undefined&&!p.async)async=p.async;if(p.serverSidePage){$.ajax({url:p.url,data:param,async:async,type:p.asyncType,dataType:'text',success:function(returnData){if(self.extractPageInfo(returnData)){if($(p.asyncTarget).size()>0)$(p.asyncTarget).empty().html(returnData);if(p.callback&&$.isFunction(p.callback))p.callback(param);self.populate();}}});}else{$.ajax({url:p.url,data:param,async:async,type:p.asyncType,dataType:'json',success:function(returnData){self.pageNumber=returnData.pageNumber;self.pageSize=returnData.pageSize;self.totalRow=returnData.totalRow;if(p.render&&$.isFunction(p.render))p.render(returnData);if(p.callback&&$.isFunction(p.callback))p.callback(param);self.populate();}});}}else{window.location.href=self.setUrl(pageNumber);}};bPage.prototype.setUrl=function(pNum){var p=this.p,str='';if(p.url){var str=p.url+'?1=1';str+='&pageNumber='+pNum;str+='&pageSize='+this.pageSize;str+=this.buildParamsStr(pNum);}else{str='javascript:void(0);';}return str;};function Plugin(p){return this.each(function(){var $this=$(this),data=$this.data('bPage'),params=$.extend({},defaults,$this.data(),typeof p=='object'&&p);if(!data)$this.data('bPage',(data=new bPage(this,params)));if($.isPlainObject(params))data.init();});}function bPageSwitch(pNum){return this.each(function(){if(!pNum||$.type(pNum)!='number')return;var $this=$(this),data=$this.data('bPage');if(data)data.pageSwitch(pNum);});}function bPageRefresh(p){return this.each(function(){var $this=$(this),data=$this.data('bPage'),params=$.extend({},defaults,$this.data(),data&&data.p,typeof p=='object'&&p);if($.isPlainObject(params))data.p=params;data.pageNumber=params.pageNumber;data.pageSize=params.pageSize;data.totalRow=params.totalRow;if(data)data.pageSwitch();});}var old=$.fn.bPage;$.fn.bPage=Plugin;$.fn.bPage.Constructor=bPage;$.fn.bPageSwitch=bPageSwitch;$.fn.bPageRefresh=bPageRefresh;$.fn.bPage.noConflict=function(){$.fn.bPage=old;return this;};}(window.jQuery);