mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
4 lines
5.7 KiB
JavaScript
4 lines
5.7 KiB
JavaScript
/* tablesorter pager plugin minified - updated 3/7/2012 */
|
|
|
|
(function(d){d.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,ajaxProcessing:function(){return[[{key:"value"}],100]},output:"{startRow} to {endRow} of {totalRows} rows",updateArrows:!0,page:0,size:10,fixedHeight:!1,removeRows:!0,cssNext:".next",cssPrev:".prev",cssFirst:".first",cssLast:".last",cssPageDisplay:".pagedisplay",cssPageSize:".pagesize",cssDisabled:"disabled",totalRows:0,totalPages:0};var h=this,i=function(c,a){var b=c.cssDisabled,e=!!a;c.updateArrows&& (c.container[c.totalRows<c.size?"addClass":"removeClass"](b),d(c.cssFirst+","+c.cssPrev,c.container)[e||0===c.page?"addClass":"removeClass"](b),d(c.cssNext+","+c.cssLast,c.container)[e||c.page===c.totalPages-1?"addClass":"removeClass"](b))},s=function(c,a){a.startRow=a.size*a.page+1;a.endRow=Math.min(a.totalRows,a.size*(a.page+1));var b=d(a.cssPageDisplay,a.container),e=a.output.replace(/\{(page|totalPages|startRow|endRow|totalRows)\}/gi,function(b){return{"{page}":a.page+1,"{totalPages}":a.totalPages, "{startRow}":a.startRow,"{endRow}":a.endRow,"{totalRows}":a.totalRows}[b]});if(b[0])b["INPUT"===b[0].tagName?"val":"html"](e);i(a);d(c).trigger("pagerComplete",a)},n=function(c,a){var b,e=d(c.tBodies[0]);if(a.fixedHeight&&(e.find("tr.pagerSavedHeightSpacer").remove(),b=d.data(c,"pagerSavedHeight")))b-=e.height(),5<b&&d.data(c,"pagerLastSize")===a.size&&e.find("tr:visible").length<a.size&&e.append('<tr class="pagerSavedHeightSpacer remove-me" style="height:'+b+'px;"></tr>')},p=function(c,a){var b, e=d("tr:not(."+c.config.cssChildRow+")",c.tBodies[0]),g=e.length,f=a.page*a.size,m=f+a.size;m>g&&(m=g);for(b=0;b<g;b++)e[b].style.display=b>=f&&b<m?"":"none"},t=function(c,a){a.size=parseInt(d(a.cssPageSize,a.container).val(),10);d.data(c,"pagerLastSize",a.size);i(a);a.removeRows||(p(c,a),d(c).bind("sortEnd.pager",function(){p(c,a);d(c).trigger("applyWidgets")}))},u=function(c,a){var b,e=d(c),g=d(c.tBodies[0]),f=a.ajaxUrl.replace(/\{page\}/g,a.page).replace(/\{size\}/g,a.size);""!==f&&(b=d("<div/>", {id:"tablesorterPagerLoading",width:e.outerWidth(!0),height:e.outerHeight(!0)}),e.before(b),d.getJSON(f,function(f){if("function"===typeof a.ajaxProcessing){var f=a.ajaxProcessing(f),q=f[0],j=q.length,l,o,h=[],k="",i="";for(l=0;l<j;l++){k+="<tr>";for(o in q[l])"string"===typeof o&&(0===l&&h.push(o),k+="<td>"+q[l][o]+"</td>");k+="</tr>"}e.find("thead th").each(function(a){var b=d(this);b.find("span").length&&b.find("span:first");b.html(h[a]);i+="<th>"+h[a]+"</th>"});e.find("tfoot").html("<tr>"+i+"</tr>"); g.html(k);b.remove();e.trigger("update");a.totalRows=f[1];a.totalPages=Math.ceil(a.totalRows/a.size);s(c,a);n(c,a);e.trigger("pagerChange",a)}}))},r=function(c,a,b){var e,g,f,m=d(c.tBodies[0]),h=a.length;e=b.page*b.size;var i=e+b.size;if(!(1>h)){d(c).trigger("pagerChange",b);if(b.removeRows){i>a.length&&(i=a.length);for(d(c.tBodies[0]).empty();e<i;e++){f=a[e];h=f.length;for(g=0;g<h;g++)m[0].appendChild(f[g])}}else p(c,b);d(c).trigger("applyWidgets");b.page>=b.totalPages&&(b.page=b.totalPages-1,j(c, b));s(c,b);b.isDisabled||n(c,b)}},v=function(c,a){a.ajax?i(a,!0):(a.isDisabled=!0,d.data(c,"pagerLastPage",a.page),d.data(c,"pagerLastSize",a.size),a.page=0,a.size=a.totalRows,a.totalPages=1,d("tr.pagerSavedHeightSpacer",c.tBodies[0]).remove(),r(c,c.config.rowsCopy,a));d(a.cssPageSize,a.container).addClass(a.cssDisabled)[0].disabled=!0},j=function(c,a){if(!a.isDisabled){if(0>a.page||a.page>a.totalPages-1)a.page=0;d.data(c,"pagerLastPage",a.page);a.ajax?u(c,a):r(c,c.config.rowsCopy,a)}},w=function(c, a,b){b.size=a;d.data(c,"pagerLastPage",b.page);d.data(c,"pagerLastSize",b.size);b.totalPages=Math.ceil(b.totalRows/b.size);j(c,b)},x=function(c,a,b){var e=d(a.cssPageSize,a.container).removeClass(a.cssDisabled).removeAttr("disabled");a.isDisabled=!1;a.page=d.data(c,"pagerLastPage")||0;a.size=d.data(c,"pagerLastSize")||parseInt(e.val(),10);a.totalPages=Math.ceil(a.totalRows/a.size);b&&(d("table").trigger("update"),w(c,a.size,a),t(c,a),n(c,a))};h.appender=function(c,a){var b=c.config.pager;b.ajax|| (c.config.rowsCopy=a,b.totalRows=a.length,b.size=d.data(c,"pagerLastSize")||b.size,b.totalPages=Math.ceil(b.totalRows/b.size),r(c,a,b))};h.construct=function(c){return this.each(function(){var a=this.config,b=a.pager=d.extend({},d.tablesorterPager.defaults,c),e=this,g=d(e),f=d(b.container).show();a.appender=h.appender;x(e,b,!1);"string"===typeof b.ajaxUrl?(b.ajax=!0,u(e,b)):(b.ajax=!1,d(this).trigger("appendCache"),t(e,b));d(b.cssFirst,f).unbind("click.pager").bind("click.pager",function(){if(!d(this).hasClass(b.cssDisabled)){b.page= 0;j(e,b)}return false});d(b.cssNext,f).unbind("click.pager").bind("click.pager",function(){if(!d(this).hasClass(b.cssDisabled)){b.page++;if(b.page>=b.totalPages-1)b.page=b.totalPages-1;j(e,b)}return false});d(b.cssPrev,f).unbind("click.pager").bind("click.pager",function(){if(!d(this).hasClass(b.cssDisabled)){b.page--;if(b.page<=0)b.page=0;j(e,b)}return false});d(b.cssLast,f).unbind("click.pager").bind("click.pager",function(){if(!d(this).hasClass(b.cssDisabled)){b.page=b.totalPages-1;j(e,b)}return false}); d(b.cssPageSize,f).unbind("change.pager").bind("change.pager",function(){d(b.cssPageSize,f).val(d(this).val());if(!d(this).hasClass(b.cssDisabled)){w(e,parseInt(d(this).val(),10),b);var a=d(e.tBodies[0]);a.find("tr.pagerSavedHeightSpacer").remove();d.data(e,"pagerSavedHeight",a.height());n(e,b);d.data(e,"pagerLastSize",b.size)}return false});g.unbind("disable.pager enable.pager destroy.pager").bind("disable.pager",function(){v(e,b)}).bind("enable.pager",function(){x(e,b,true)}).bind("destroy.pager", function(){v(e,b);b.container.hide();e.config.appender=null;d(e).unbind("destroy.pager sortEnd.pager enable.pager disable.pager")})})}}});d.fn.extend({tablesorterPager:d.tablesorterPager.construct})})(jQuery);
|