mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
3 lines
12 KiB
JavaScript
3 lines
12 KiB
JavaScript
/*! tablesorter pager plugin minified - updated 12/2/2013 (v2.14.3) */
|
|
;(function(h){var k=h.tablesorter;h.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,customAjaxUrl:function(b,a){return a},ajaxObject:{dataType:"json"},ajaxProcessing:function(b){return[0,[],null]},output:"{startRow} to {endRow} of {totalRows} rows",updateArrows:!0,page:0,size:10,savePages:!0,fixedHeight:!1,countChildRows:!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,ajaxCounter:0,currentFilters:[],startRow:0,endRow:0,$size:null,last:{}};var v=this,w=function(b,a){var c=b.cssDisabled,e=!!a,f=Math.min(b.totalPages,b.filteredPages);b.updateArrows&&(b.$container.find(b.cssFirst+","+b.cssPrev)[e||0===b.page?"addClass":"removeClass"](c),b.$container.find(b.cssNext+","+b.cssLast)[e||b.page===f-1?"addClass":"removeClass"](c))},r=function(b,a,c){var e,f,g, d=b.config;e=d.$table.hasClass("hasFilters")&&!a.ajaxUrl;g=(d.widgetOptions&&d.widgetOptions.filter_filteredRow||"filtered")+","+d.selectorRemove;f=a.size||10;a.totalPages=Math.ceil(a.totalRows/f);a.filteredRows=e?d.$tbodies.eq(0).children("tr:not(."+g+")").length:a.totalRows;a.filteredPages=e?Math.ceil(a.filteredRows/f)||1:a.totalPages;if(0<=Math.min(a.totalPages,a.filteredPages)&&(g=a.size*a.page>a.filteredRows,a.startRow=g?1:0===a.filteredRows?0:a.size*a.page+1,a.page=g?0:a.page,a.endRow=Math.min(a.filteredRows, a.totalRows,a.size*(a.page+1)),e=a.$container.find(a.cssPageDisplay),g=(a.ajaxData&&a.ajaxData.output?a.ajaxData.output||a.output:a.output).replace(/\{page([\-+]\d+)?\}/gi,function(b,d){return a.page+(d?parseInt(d,10):1)}).replace(/\{\w+(\s*:\s*\w+)?\}/gi,function(b){b=b.replace(/[{}\s]/g,"");var d=b.split(":"),c=a.ajaxData,e=/(rows?|pages?)$/i.test(b)?0:"";return 1<d.length&&c&&c[d[0]]?c[d[0]][d[1]]:a[b]||(c?c[b]:e)||e}),e.length&&(e["INPUT"===e[0].tagName?"val":"html"](g),a.$goto.length))){g=""; f=Math.min(a.totalPages,a.filteredPages);for(e=1;e<=f;e++)g+="<option>"+e+"</option>";a.$goto.html(g).val(a.page+1)}w(a);a.initialized&&!1!==c&&(d.$table.trigger("pagerComplete",a),a.savePages&&k.storage&&k.storage(b,"tablesorter-pager",{page:a.page,size:a.size}))},t=function(b,a){var c,e=b.config,f=e.$tbodies.eq(0);a.fixedHeight&&(f.find("tr.pagerSavedHeightSpacer").remove(),c=h.data(b,"pagerSavedHeight"))&&(c-=f.height(),5<c&&h.data(b,"pagerLastSize")===a.size&&f.children("tr:visible").length<a.size&& f.append('<tr class="pagerSavedHeightSpacer '+e.selectorRemove.replace(/(tr)?\./g,"")+'" style="height:'+c+'px;"></tr>'))},A=function(b,a){var c=b.config.$tbodies.eq(0);c.find("tr.pagerSavedHeightSpacer").remove();h.data(b,"pagerSavedHeight",c.height());t(b,a);h.data(b,"pagerLastSize",a.size)},u=function(b,a){if(!a.ajaxUrl){var c,e=b.config,f=e.$tbodies.eq(0).children(),g=f.length,d=a.page*a.size,h=d+a.size,k=e.widgetOptions&&e.widgetOptions.filter_filteredRow||"filtered",n=0;for(c=0;c<g;c++)f[c].className.match(k)|| (f[c].style.display=n>=d&&n<h?"":"none",n+=f[c].className.match(e.cssChildRow+"|"+e.selectorRemove.slice(1))&&!a.countChildRows?0:1)}},B=function(b,a){a.size=parseInt(a.$size.val(),10)||a.size;h.data(b,"pagerLastSize",a.size);w(a);a.removeRows||(u(b,a),h(b).bind("sortEnd.pager filterEnd.pager",function(){u(b,a)}))},C=function(b,a,c,e,f){if("function"===typeof c.ajaxProcessing){var g,d,s,m,n,x,p=a.config,l=p.$table,q="";g=c.ajaxProcessing(b,a)||[0,[]];b=l.find("thead th").length;l.find("thead tr."+ c.cssErrorRow).remove();if(f)p.debug&&k.log("Ajax Error",e,f),h('<tr class="'+c.cssErrorRow+'"><td style="text-align:center;" colspan="'+b+'">'+(0===e.status?"Not connected, verify Network":404===e.status?"Requested page not found [404]":500===e.status?"Internal Server Error [500]":"parsererror"===f?"Requested JSON parse failed":"timeout"===f?"Time out error":"abort"===f?"Ajax Request aborted":"Uncaught error: "+e.statusText+" ["+e.status+"]")+"</td></tr>").click(function(){h(this).remove()}).appendTo(l.find("thead:first")), p.$tbodies.eq(0).empty();else{h.isArray(g)?(e=isNaN(g[0])&&!isNaN(g[1]),f=g[e?1:0],c.totalRows=isNaN(f)?c.totalRows||0:f,e=g[e?0:1]||[],n=g[2]):(c.ajaxData=g,c.totalRows=g.total,n=g.headers,e=g.rows);x=e.length;if(e instanceof jQuery)p.$tbodies.eq(0).empty().append(e);else if(x){for(g=0;g<x;g++){q+="<tr>";for(f=0;f<e[g].length;f++)q+=/^\s*<td/.test(e[g][f])?h.trim(e[g][f]):"<td>"+e[g][f]+"</td>";q+="</tr>"}p.$tbodies.eq(0).html(q)}n&&n.length===b&&(m=(d=l.hasClass("hasStickyHeaders"))?p.$sticky.children("thead:first").children().children(): "",s=l.find("tfoot tr:first").children(),p.$headers.filter("th").each(function(a){var b=h(this),c;b.find("."+k.css.icon).length?(c=b.find("."+k.css.icon).clone(!0),b.find(".tablesorter-header-inner").html(n[a]).append(c),d&&m.length&&(c=m.eq(a).find("."+k.css.icon).clone(!0),m.eq(a).find(".tablesorter-header-inner").html(n[a]).append(c))):(b.find(".tablesorter-header-inner").html(n[a]),d&&m.length&&m.eq(a).find(".tablesorter-header-inner").html(n[a]));s.eq(a).html(n[a])}))}p.showProcessing&&k.isProcessing(a); c.last.totalPages=c.totalPages=Math.ceil(c.totalRows/(c.size||10));c.last.currentFilters=c.currentFilters;c.last.sortList=(p.sortList||[]).join(",");r(a,c);t(a,c);l.trigger("applyWidgets");c.initialized?(l.trigger("pagerChange",c),l.trigger("updateComplete")):l.trigger("update")}c.initialized||(c.initialized=!0,h(a).trigger("pagerInitialized",c))},H=function(b,a){var c=G(b,a),e=h(document),f,g=b.config;""!==c&&(g.showProcessing&&k.isProcessing(b,!0),e.bind("ajaxError.pager",function(d,c,f,g){C(null, b,a,c,g);e.unbind("ajaxError.pager")}),f=++a.ajaxCounter,a.ajaxObject.url=c,a.ajaxObject.success=function(c){f<a.ajaxCounter||(C(c,b,a),e.unbind("ajaxError.pager"),"function"===typeof a.oldAjaxSuccess&&a.oldAjaxSuccess(c))},g.debug&&k.log("ajax initialized",a.ajaxObject),h.ajax(a.ajaxObject))},G=function(b,a){var c=b.config,e=a.ajaxUrl?a.ajaxUrl.replace(/\{page([\-+]\d+)?\}/,function(b,c){return a.page+(c?parseInt(c,10):0)}).replace(/\{size\}/g,a.size):"",f=c.sortList,g=a.currentFilters||h(b).data("lastSearch")|| [],d=e.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),l=e.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),m=[];d&&(d=d[1],h.each(f,function(a,b){m.push(d+"["+b[0]+"]="+b[1])}),e=e.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):d),m=[]);l&&(l=l[1],h.each(g,function(a,b){b&&m.push(l+"["+a+"]="+encodeURIComponent(b))}),e=e.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):l),a.currentFilters=g);"function"===typeof a.customAjaxUrl&&(e=a.customAjaxUrl(b,e));c.debug&& k.log("Pager ajax url: "+e);return e},y=function(b,a,c){var e,f;e=c.page*c.size;var g=e+c.size;if(!(1>(a&&a.length||0))){c.page>=c.totalPages&&D(b,c);c.isDisabled=!1;c.initialized&&h(b).trigger("pagerChange",c);if(c.removeRows){g>a.length&&(g=a.length);k.clearTableBody(b);for(f=k.processTbody(b,b.config.$tbodies.eq(0),!0);e<g;e++)f.append(a[e]);k.processTbody(b,f,!1)}else u(b,c);r(b,c);c.isDisabled||t(b,c);h(b).trigger("applyWidgets")}},E=function(b,a){a.ajax?w(a,!0):(a.isDisabled=!0,h.data(b,"pagerLastPage", a.page),h.data(b,"pagerLastSize",a.size),a.page=0,a.size=a.totalRows,a.totalPages=1,h(b).addClass("pagerDisabled").find("tr.pagerSavedHeightSpacer").remove(),y(b,b.config.rowsCopy,a),b.config.debug&&k.log("pager disabled"));a.$size.add(a.$goto).each(function(){h(this).addClass(a.cssDisabled)[0].disabled=!0})},l=function(b,a,c){if(!a.isDisabled){var e=b.config,f=a.last,g=Math.min(a.totalPages,a.filteredPages);0>a.page&&(a.page=0);a.page>g-1&&0!==g&&(a.page=g-1);if(f.page!==a.page||f.size!==a.size|| f.totalPages!==a.totalPages||(f.currentFilters||[]).join(",")!==(a.currentFilters||[]).join(",")||f.sortList!==(e.sortList||[]).join(","))e.debug&&k.log("Pager changing to page "+a.page),a.last={page:a.page,size:a.size,sortList:(e.sortList||[]).join(","),totalPages:a.totalPages,currentFilters:a.currentFilters||[]},a.ajax?H(b,a):a.ajax||y(b,b.config.rowsCopy,a),h.data(b,"pagerLastPage",a.page),a.initialized&&!1!==c&&(e.$table.trigger("pageMoved",a),e.$table.trigger("applyWidgets"))}},z=function(b, a,c){c.size=a||c.size||10;c.$size.val(c.size);h.data(b,"pagerLastPage",c.page);h.data(b,"pagerLastSize",c.size);c.totalPages=Math.ceil(c.totalRows/c.size);l(b,c)},I=function(b,a){a.page=0;l(b,a)},D=function(b,a){a.page=Math.min(a.totalPages,a.filteredPages)-1;l(b,a)},J=function(b,a){a.page++;a.page>=Math.min(a.totalPages,a.filteredPages)-1&&(a.page=Math.min(a.totalPages,a.filteredPages)-1);l(b,a)},K=function(b,a){a.page--;0>=a.page&&(a.page=0);l(b,a)},F=function(b,a,c){var e=a.$size.removeClass(a.cssDisabled).removeAttr("disabled"); a.$goto.removeClass(a.cssDisabled).removeAttr("disabled");a.isDisabled=!1;a.page=h.data(b,"pagerLastPage")||a.page||0;a.size=h.data(b,"pagerLastSize")||parseInt(e.find("option[selected]").val(),10)||a.size||10;e.val(a.size);a.totalPages=Math.ceil(Math.min(a.totalPages,a.filteredPages)/a.size);c&&(h(b).trigger("update"),z(b,a.size,a),B(b,a),t(b,a),b.config.debug&&k.log("pager enabled"))};v.appender=function(b,a){var c=b.config,e=c.pager;e.ajax||(c.rowsCopy=a,e.totalRows=e.countChildRows?c.$tbodies.eq(0).children().length: a.length,e.size=h.data(b,"pagerLastSize")||e.size||10,e.totalPages=Math.ceil(e.totalRows/e.size),y(b,a,e))};v.construct=function(b){return this.each(function(){if(this.config&&this.hasInitialized){var a,c,e,f=this,g=f.config,d=g.pager=h.extend({},h.tablesorterPager.defaults,b),s=g.$table,m=d.$container=h(d.container).addClass("tablesorter-pager").show();g.debug&&k.log("Pager initializing");d.oldAjaxSuccess=d.oldAjaxSuccess||d.ajaxObject.success;g.appender=v.appender;k.filter&&0<=h.inArray("filter", g.widgets)&&(d.currentFilters=g.$table.data("lastSearch")||k.filter.setDefaults(f,g,g.widgetOptions)||[],k.setFilters(f,d.currentFilters,!1));d.savePages&&k.storage&&(a=k.storage(f,"tablesorter-pager")||{},d.page=isNaN(a.page)?d.page:a.page,d.size=(isNaN(a.size)?d.size:a.size)||10,h.data(f,"pagerLastSize",d.size));s.unbind("filterStart filterEnd sortEnd disable enable destroy update pageSize ".split(" ").join(".pager ")).bind("filterStart.pager",function(a,b){d.currentFilters=b}).bind("filterEnd.pager sortEnd.pager", function(){d.initialized&&(l(f,d,!1),r(f,d,!1),t(f,d))}).bind("disable.pager",function(a){a.stopPropagation();E(f,d)}).bind("enable.pager",function(a){a.stopPropagation();F(f,d,!0)}).bind("destroy.pager",function(a){a.stopPropagation();E(f,d);d.$container.hide();f.config.appender=null;d.initialized=!1;h(f).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager");k.storage&&k.storage(f,"tablesorter-pager","")}).bind("update.pager",function(a){a.stopPropagation();u(f,d)}).bind("pageSize.pager", function(a,b){a.stopPropagation();z(f,parseInt(b,10)||10,d);u(f,d);r(f,d,!1);d.$size.length&&d.$size.val(d.size)}).bind("pageSet.pager",function(a,b){a.stopPropagation();d.page=(parseInt(b,10)||1)-1;d.$goto.length&&d.$goto.val(d.size);l(f,d);r(f,d,!1)});c=[d.cssFirst,d.cssPrev,d.cssNext,d.cssLast];e=[I,K,J,D];m.find(c.join(",")).unbind("click.pager").bind("click.pager",function(a){a.stopPropagation();var b=h(this),g=c.length;if(!b.hasClass(d.cssDisabled))for(a=0;a<g;a++)if(b.is(c[a])){e[a](f,d);break}}); d.$goto=m.find(d.cssGoto);d.$goto.length&&d.$goto.unbind("change").bind("change",function(){d.page=h(this).val()-1;l(f,d);r(f,d,!1)});d.$size=m.find(d.cssPageSize);d.$size.length&&d.$size.unbind("change.pager").bind("change.pager",function(){d.$size.val(h(this).val());h(this).hasClass(d.cssDisabled)||(z(f,parseInt(h(this).val(),10),d),A(f,d));return!1});d.initialized=!1;s.trigger("pagerBeforeInitialized",d);F(f,d,!1);"string"===typeof d.ajaxUrl?(d.ajax=!0,g.widgetOptions.filter_serversideFiltering= !0,g.serverSideSorting=!0,l(f,d)):(d.ajax=!1,h(this).trigger("appendCache",!0),B(f,d));A(f,d);d.ajax||(d.initialized=!0,h(f).trigger("pagerInitialized",d))}})}}});h.fn.extend({tablesorterPager:h.tablesorterPager.construct})})(jQuery);
|