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