mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
3 lines
9.2 KiB
JavaScript
3 lines
9.2 KiB
JavaScript
/*! tablesorter pager plugin minified - updated 1/29/2013 */
|
|
;(function(d){d.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,ajaxProcessing:function(){return[0,[],null]},output:"{startRow} to {endRow} of {totalRows} rows",updateArrows:!0,page:0,size:10,fixedHeight:!1,removeRows:!1,cssFirst:".first",cssPrev:".prev",cssNext:".next",cssLast:".last",cssGoto:".gotoPage",cssPageDisplay:".pagedisplay",cssPageSize:".pagesize",cssErrorRow:"tablesorter-errorRow",cssDisabled:"disabled",totalRows:0,totalPages:0,filteredRows:0,filteredPages:0}; var p=this,s=function(b,a){var c=b.cssDisabled,g=!!a,h=Math.min(b.totalPages,b.filteredPages);b.updateArrows&&(d(b.cssFirst+","+b.cssPrev,b.container)[g||0===b.page?"addClass":"removeClass"](c),d(b.cssNext+","+b.cssLast,b.container)[g||b.page===h-1?"addClass":"removeClass"](c))},t=function(b,a){var c,g,h;c=d(b).hasClass("hasFilters")&&!a.ajaxUrl;a.filteredRows=c?b.config.$tbodies.children("tr:not(.filtered,.remove-me)").length:a.totalRows;a.filteredPages=c?Math.ceil(a.filteredRows/a.size):a.totalPages; if(0<Math.min(a.totalPages,a.filteredPages)&&(h=a.size*a.page>a.filteredRows,a.startRow=h?1:a.size*a.page+1,a.page=h?0:a.page,a.endRow=Math.min(a.filteredRows,a.totalRows,a.size*(a.page+1)),g=d(a.cssPageDisplay,a.container),c=a.output.replace(/\{(page|filteredRows|filteredPages|totalPages|startRow|endRow|totalRows)\}/gi,function(b){return{"{page}":a.page+1,"{filteredRows}":a.filteredRows,"{filteredPages}":a.filteredPages,"{totalPages}":a.totalPages,"{startRow}":a.startRow,"{endRow}":a.endRow,"{totalRows}":a.totalRows}[b]}), g[0]&&(g["INPUT"===g[0].tagName?"val":"html"](c),d(a.cssGoto,a.container).length))){h="";g=Math.min(a.totalPages,a.filteredPages);for(c=1;c<=g;c++)h+="<option>"+c+"</option>";d(a.cssGoto,a.container).html(h).val(a.page+1)}s(a);a.initialized&&d(b).trigger("pagerComplete",a)},u=function(b,a){var c,g=d(b.tBodies[0]);if(a.fixedHeight&&(g.find("tr.pagerSavedHeightSpacer").remove(),c=d.data(b,"pagerSavedHeight")))c-=g.height(),5<c&&(d.data(b,"pagerLastSize")===a.size&&g.children("tr:visible").length<a.size)&& g.append('<tr class="pagerSavedHeightSpacer '+b.config.selectorRemove.replace(/(tr)?\./g,"")+'" style="height:'+c+'px;"></tr>')},w=function(b,a){var c=d(b.tBodies[0]);c.find("tr.pagerSavedHeightSpacer").remove();d.data(b,"pagerSavedHeight",c.height());u(b,a);d.data(b,"pagerLastSize",a.size)},n=function(b,a){if(!a.ajaxUrl){var c,g=d(b.tBodies).children("tr:not(."+b.config.cssChildRow+")"),h=g.length,e=a.page*a.size,f=e+a.size,k=0;for(c=0;c<h;c++)/filtered/.test(g[c].className)||(g[c].style.display= k>=e&&k<f?"":"none",k++)}},x=function(b,a){a.size=parseInt(d(a.cssPageSize,a.container).find("option[selected]").val(),10)||a.size;d.data(b,"pagerLastSize",a.size);s(a);a.removeRows||(n(b,a),d(b).bind("sortEnd.pager filterEnd.pager",function(){n(b,a)}))},z=function(b,a,c,g){if("function"===typeof c.ajaxProcessing){var h,e,f,k,j=d(a),l=a.config,y=j.find("thead th").length,q="",m='<tr class="'+c.cssErrorRow+" "+l.selectorRemove.replace(/(tr)?\./g,"")+'"><td style="text-align: center;" colspan="'+y+ '">'+(g?g.message+" ("+g.name+")":"No rows found")+"</td></tr>",n=c.ajaxProcessing(b)||[0,[]],p=n[1]||[],s=p.length,r=n[2];if(0<s)for(b=0;b<s;b++){q+="<tr>";for(h=0;h<p[b].length;h++)q+="<td>"+p[b][h]+"</td>";q+="</tr>"}r&&r.length===y&&(e=j.hasClass("hasStickyHeaders"),k=j.find("."+(l.widgetOptions&&l.widgetOptions.stickyHeaders||"tablesorter-stickyheader")),f=j.find("tfoot tr:first").children(),j.find("th."+l.cssHeader).each(function(a){var b=d(this),c;b.find("."+l.cssIcon).length?(c=b.find("."+ l.cssIcon).clone(!0),b.find(".tablesorter-header-inner").html(r[a]).append(c),e&&k.length&&(c=k.find("th").eq(a).find("."+l.cssIcon).clone(!0),k.find("th").eq(a).find(".tablesorter-header-inner").html(r[a]).append(c))):(b.find(".tablesorter-header-inner").html(r[a]),k.find("th").eq(a).find(".tablesorter-header-inner").html(r[a]));f.eq(a).html(r[a])}));j.find("thead tr."+c.cssErrorRow).remove();g?j.find("thead").append(m):d(a.tBodies[0]).html(q);l.showProcessing&&d.tablesorter.isProcessing(a);j.trigger("update"); c.totalRows=n[0]||0;c.totalPages=Math.ceil(c.totalRows/c.size);t(a,c);u(a,c);c.initialized&&j.trigger("pagerChange",c)}c.initialized||(c.initialized=!0,d(a).trigger("pagerInitialized",c))},v=function(b,a,c){c.isDisabled=!1;var g,h,e,f=document.createDocumentFragment(),k=a.length;g=c.page*c.size;var j=g+c.size;if(!(1>k)){c.initialized&&d(b).trigger("pagerChange",c);if(c.removeRows){j>a.length&&(j=a.length);d(b.tBodies[0]).addClass("tablesorter-hidden");for(d.tablesorter.clearTableBody(b);g<j;g++){e= a[g];k=e.length;for(h=0;h<k;h++)f.appendChild(e[h])}b.tBodies[0].appendChild(f);d(b.tBodies[0]).removeClass("tablesorter-hidden")}else n(b,c);c.page>=c.totalPages&&A(b,c);t(b,c);c.isDisabled||u(b,c);d(b).trigger("applyWidgets")}},B=function(b,a){a.ajax?s(a,!0):(a.isDisabled=!0,d.data(b,"pagerLastPage",a.page),d.data(b,"pagerLastSize",a.size),a.page=0,a.size=a.totalRows,a.totalPages=1,d("tr.pagerSavedHeightSpacer",b.tBodies[0]).remove(),v(b,b.config.rowsCopy,a));d(a.container).find(a.cssPageSize+","+ a.cssGoto).each(function(){d(this).addClass(a.cssDisabled)[0].disabled=!0})},m=function(b,a){if(!a.isDisabled){var c=Math.min(a.totalPages,a.filteredPages);if(0>a.page||a.page>c-1)a.page=0;if(a.ajax){var g,c=a.ajaxUrl?a.ajaxUrl.replace(/\{page\}/g,a.page).replace(/\{size\}/g,a.size):"",h=b.config.sortList,e=a.currentFilters||[],f=c.match(/\{sortList[\s+]?:[\s+]?([^}]*)\}/),k=c.match(/\{filterList[\s+]?:[\s+]?([^}]*)\}/),j=[];f&&(f=f[1],d.each(h,function(a,b){j.push(f+"["+b[0]+"]="+b[1])}),c=c.replace(/\{sortList[\s+]?:[\s+]?([^\}]*)\}/g, j.length?j.join("&"):f));k&&(k=k[1],d.each(e,function(a,b){b&&j.push(k+"["+a+"]="+encodeURIComponent(b))}),c=c.replace(/\{filterList[\s+]?:[\s+]?([^\}]*)\}/g,j.length?j.join("&"):k));g=c;c=b.config;""!==g&&(c.showProcessing&&d.tablesorter.isProcessing(b,!0),d(document).bind("ajaxError.pager",function(c,e,f,h){f.url===g&&(z(null,b,a,h),d(document).unbind("ajaxError.pager"))}),d.getJSON(g,function(c){z(c,b,a);d(document).unbind("ajaxError.pager")}))}else a.ajax||v(b,b.config.rowsCopy,a);d.data(b,"pagerLastPage", a.page);d.data(b,"pagerUpdateTriggered",!0);a.initialized&&d(b).trigger("pageMoved",a)}},C=function(b,a,c){c.size=a;d.data(b,"pagerLastPage",c.page);d.data(b,"pagerLastSize",c.size);c.totalPages=Math.ceil(c.totalRows/c.size);m(b,c)},E=function(b,a){a.page=0;m(b,a)},A=function(b,a){a.page=Math.min(a.totalPages,a.filteredPages)-1;m(b,a)},F=function(b,a){a.page++;a.page>=Math.min(a.totalPages,a.filteredPages)-1&&(a.page=Math.min(a.totalPages,a.filteredPages)-1);m(b,a)},G=function(b,a){a.page--;0>=a.page&& (a.page=0);m(b,a)},D=function(b,a,c){var g=d(a.cssPageSize,a.container).removeClass(a.cssDisabled).removeAttr("disabled");d(a.container).find(a.cssGoto).removeClass(a.cssDisabled).removeAttr("disabled");a.isDisabled=!1;a.page=d.data(b,"pagerLastPage")||a.page||0;a.size=d.data(b,"pagerLastSize")||parseInt(g.find("option[selected]").val(),10)||a.size;g.val(a.size);a.totalPages=Math.ceil(Math.min(a.totalPages,a.filteredPages)/a.size);c&&(d(b).trigger("update"),C(b,a.size,a),x(b,a),u(b,a))};p.appender= function(b,a){var c=b.config.pager;c.ajax||(b.config.rowsCopy=a,c.totalRows=a.length,c.size=d.data(b,"pagerLastSize")||c.size,c.totalPages=Math.ceil(c.totalRows/c.size),v(b,a,c))};p.construct=function(b){return this.each(function(){if(this.config&&this.hasInitialized){var a,c,g,h=this.config,e=h.pager=d.extend({},d.tablesorterPager.defaults,b),f=this,k=f.config,j=d(f),l=d(e.container).addClass("tablesorter-pager").show();h.appender=p.appender;j.unbind("filterStart.pager filterEnd.pager sortEnd.pager disable.pager enable.pager destroy.pager update.pager pageSize.pager").bind("filterStart.pager", function(a,b){d.data(f,"pagerUpdateTriggered",!1);e.currentFilters=b}).bind("filterEnd.pager sortEnd.pager",function(a){d.data(f,"pagerUpdateTriggered")?d.data(f,"pagerUpdateTriggered",!1):("filterEnd"===a.type&&(e.page=0),t(f,e),m(f,e),u(f,e))}).bind("disable.pager",function(){B(f,e)}).bind("enable.pager",function(){D(f,e,!0)}).bind("destroy.pager",function(){B(f,e);d(e.container).hide();f.config.appender=null;d(f).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager")}).bind("update.pager", function(){n(f,e)}).bind("pageSize.pager",function(a,b){e.size=parseInt(b,10)||10;n(f,e);t(f,e)});c=[e.cssFirst,e.cssPrev,e.cssNext,e.cssLast];g=[E,G,F,A];l.find(c.join(",")).unbind("click.pager").bind("click.pager",function(){var a,b=d(this),h=c.length;if(!b.hasClass(e.cssDisabled))for(a=0;a<h;a++)if(b.is(c[a])){g[a](f,e);break}return!1});l.find(e.cssGoto).length&&(l.find(e.cssGoto).unbind("change").bind("change",function(){e.page=d(this).val()-1;m(f,e)}),t(f,e));a=l.find(e.cssPageSize);a.length&& a.unbind("change.pager").bind("change.pager",function(){a.val(d(this).val());d(this).hasClass(e.cssDisabled)||(C(f,parseInt(d(this).val(),10),e),w(f,e));return!1});e.initialized=!1;j.trigger("pagerBeforeInitialized",e);D(f,e,!1);"string"===typeof e.ajaxUrl?(e.ajax=!0,k.widgetOptions.filter_serversideFiltering=!0,k.serverSideSorting=!0,m(f,e)):(e.ajax=!1,d(this).trigger("appendCache",!0),x(f,e));w(f,e);e.ajax||(e.initialized=!0,d(f).trigger("pagerInitialized",e))}})}}});d.fn.extend({tablesorterPager:d.tablesorterPager.construct})})(jQuery);
|