2 lines
2.6 KiB
JavaScript
2 lines
2.6 KiB
JavaScript
(function(o,l,f,v){var c=function(e,t){this.ele=e,this.defaults={currentPage:1,totalPage:10,isShow:!0,count:5,homePageText:"\u9996\u9875",endPageText:"\u5C3E\u9875",callback:function(){}},this.opts=o.extend({},this.defaults,t),this.current=this.opts.currentPage,this.total=this.opts.totalPage,this.init()};c.prototype={init:function(){this.render(),this.eventBind()},render:function(){var e=this.opts,t=this.current,i=this.total,n=this.getPagesTpl(),r=this.ele.empty();this.isRender=!0,this.homePage='<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="1">'+e.homePageText+"</a>",this.endPage='<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="'+i+'">'+e.endPageText+"</a>",this.checkPage(),this.isRender&&r.html("<div class='ui-pagination-container'>"+this.homePage+n+this.endPage+"</div>")},checkPage:function(){var e=this.opts,t=this.total,i=this.current;e.isShow||(this.homePage=this.endPage=""),i===1&&(this.homePage=this.prevPage=""),i===t&&(this.endPage=this.nextPage=""),t===1&&(this.homePage=this.prevPage=this.endPage=this.nextPage=""),t<=1&&(this.isRender=!1)},getPagesTpl:function(){var e=this.opts,t=this.total,i=this.current,n="",r=e.count;if(t<=r)for(a=1;a<=t;a++)n+=a===i?'<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="'+a+'">'+a+"</a>":'<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="'+a+'">'+a+"</a>";else{var u=r/2;if(i<=u)for(a=1;a<=r;a++)n+=a===i?'<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="'+a+'">'+a+"</a>":'<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="'+a+'">'+a+"</a>";else{var p=Math.floor(u),s=i+p,h=i-p,d=r%2==0;s>t&&(d?(h-=s-t-1,s=t+1):(h-=s-t,s=t)),d||s++;for(var a=h;a<s;a++)n+=a===i?'<a href="javascript:void(0);" class="ui-pagination-page-item active" data-current="'+a+'">'+a+"</a>":'<a href="javascript:void(0);" class="ui-pagination-page-item" data-current="'+a+'">'+a+"</a>"}}return n},setPage:function(e,t){return e===this.current&&t===this.total?this.ele:(this.current=e,this.total=t,this.render(),this.ele)},getPage:function(){return{current:this.current,total:this.total}},eventBind:function(){var e=this,t=this.opts.callback;this.ele.off("click").on("click",".ui-pagination-page-item",function(){var i=o(this).data("current");e.current!=i&&(e.current=i,e.render(),t&&typeof t=="function"&&t(i))})}},o.fn.pagination=function(e,t,i){if(typeof e=="object"){var n=new c(this,e);this.data("pagination",n)}return typeof e=="string"?this.data("pagination")[e](t,i):this}})(jQuery,window,document);
|