mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
3 lines
14 KiB
JavaScript
3 lines
14 KiB
JavaScript
/* tablesorter pager plugin updated 5/28/2014 (v2.17.1) */
|
|
;(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,pageReset: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 w=this,p=function(b,a){var c=b.cssDisabled,e=!!a,f=e||0===b.page,g=Math.min(b.totalPages,b.filteredPages),e=e||b.page===g-1||0===b.totalPages;b.updateArrows&&(b.$container.find(b.cssFirst+","+b.cssPrev)[f?"addClass":"removeClass"](c).attr("aria-disabled", f),b.$container.find(b.cssNext+","+b.cssLast)[e?"addClass":"removeClass"](c).attr("aria-disabled",e))},t=function(b,a,c){var e,f,g,l=b.config;e=l.$table.hasClass("hasFilters")&&!a.ajaxUrl;g=[];f=a.size||10;g=[l.widgetOptions&&l.widgetOptions.filter_filteredRow||"filtered",l.selectorRemove];a.countChildRows&&g.push(l.cssChildRow);g.join("|");a.totalPages=Math.ceil(a.totalRows/f);a.filteredRows=e?0:a.totalRows;a.filteredPages=a.totalPages;e&&(h.each(l.cache[0].normalized,function(d,b){a.filteredRows+= a.regexRows.test(b[l.columns].$row[0].className)?0:1}),a.filteredPages=Math.ceil(a.filteredRows/f)||0);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(d,b){return a.totalPages?a.page+(b?parseInt(b, 10):1):0}).replace(/\{\w+(\s*:\s*\w+)?\}/gi,function(d){d=d.replace(/[{}\s]/g,"");var b=d.split(":"),c=a.ajaxData,e=/(rows?|pages?)$/i.test(d)?0:"";return 1<b.length&&c&&c[b[0]]?c[b[0]][b[1]]:a[d]||(c?c[d]: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)}p(a);a.initialized&&!1!==c&&(l.$table.trigger("pagerComplete",a),a.savePages&&k.storage&&k.storage(b, a.storageKey,{page:a.page,size:a.size}))},u=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>'))},z=function(b,a){var c=b.config.$tbodies.eq(0);c.find("tr.pagerSavedHeightSpacer").remove();h.data(b, "pagerSavedHeight",c.height());u(b,a);h.data(b,"pagerLastSize",a.size)},v=function(b,a){if(!a.ajaxUrl){var c,e=0,f=b.config,g=f.$tbodies.eq(0).children(),h=g.length,d=a.page*a.size,m=d+a.size,n=f.widgetOptions&&f.widgetOptions.filter_filteredRow||"filtered",r=0;for(c=0;c<h;c++)g[c].className.match(n)||(r===d&&g[c].className.match(f.cssChildRow)?g[c].style.display="none":(g[c].style.display=r>=d&&r<m?"":"none",r+=g[c].className.match(f.cssChildRow+"|"+f.selectorRemove.slice(1))&&!a.countChildRows? 0:1,r===m&&"none"!==g[c].style.display&&g[c].className.match(k.css.cssHasChild)&&(e=c)));if(0<e&&g[e].className.match(k.css.cssHasChild))for(;++e<h&&g[e].className.match(f.cssChildRow);)g[e].style.display=""}},A=function(b,a){a.size=parseInt(a.$size.val(),10)||a.size;h.data(b,"pagerLastSize",a.size);p(a);a.removeRows||(v(b,a),h(b).bind("sortEnd.pager filterEnd.pager",function(){v(b,a)}))},B=function(b,a,c,e,f){if("function"===typeof c.ajaxProcessing){var g,l,d,m,n,r,s=a.config,q=s.$table,p="";g=c.ajaxProcessing(b, a,e)||[0,[]];b=q.find("thead th").length;k.showError(a);if(f)s.debug&&k.log("Ajax Error",e,f),k.showError(a,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+"]"),s.$tbodies.eq(0).detach(),c.totalRows=0;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=0===c.totalRows?[""]:g[e?0:1]||[],n=g[2]):(c.ajaxData=g,c.totalRows=g.total,n=g.headers,e=g.rows);r=e.length;if(e instanceof jQuery)c.processAjaxOnInit&&s.$tbodies.eq(0).detach().append(e);else if(r){for(g=0;g<r;g++){p+="<tr>";for(f=0;f<e[g].length;f++)p+=/^\s*<td/.test(e[g][f])?h.trim(e[g][f]):"<td>"+e[g][f]+"</td>";p+="</tr>"}c.processAjaxOnInit&&s.$tbodies.eq(0).html(p)}c.processAjaxOnInit=!0;n&&n.length===b&&(m=(l=q.hasClass("hasStickyHeaders"))?s.widgetOptions.$sticky.children("thead:first").children().children(): "",d=q.find("tfoot tr:first").children(),s.$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),l&&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]),l&&m.length&&m.eq(a).find(".tablesorter-header-inner").html(n[a]));d.eq(a).html(n[a])}))}s.showProcessing&&k.isProcessing(a); c.totalPages=Math.ceil(c.totalRows/(c.size||10));c.last.totalRows=c.totalRows;c.last.currentFilters=c.currentFilters;c.last.sortList=(s.sortList||[]).join(",");t(a,c);u(a,c);q.trigger("updateCache",[function(){c.initialized&&setTimeout(function(){q.trigger("applyWidgets").trigger("pagerChange",c)},0)}])}c.initialized||(c.initialized=!0,h(a).trigger("applyWidgets").trigger("pagerInitialized",c))},G=function(b,a){var c=F(b,a),e=h(document),f,g=b.config;""!==c&&(g.showProcessing&&k.isProcessing(b,!0), e.bind("ajaxError.pager",function(c,d,f,g){B(null,b,a,d,g);e.unbind("ajaxError.pager")}),f=++a.ajaxCounter,a.ajaxObject.url=c,a.ajaxObject.success=function(c,d,g){f<a.ajaxCounter||(B(c,b,a,g),e.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 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")||[],l=e.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),d=e.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),m=[];l&&(l=l[1],h.each(f,function(a,b){m.push(l+"["+b[0]+"]="+b[1])}),e=e.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):l),m=[]);d&&(d=d[1],h.each(g,function(a,b){b&&m.push(d+"["+a+"]="+encodeURIComponent(b))}),e=e.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):d),a.currentFilters=g); "function"===typeof a.customAjaxUrl&&(e=a.customAjaxUrl(b,e));c.debug&&k.log("Pager ajax url: "+e);return e},x=function(b,a,c){var e,f,g,l,d=h(b);e=b.config;var m=e.$table.hasClass("hasFilters"),n=c.page*c.size,r=c.size;if(!(1>(a&&a.length||0))){c.page>=c.totalPages&&C(b,c);c.isDisabled=!1;c.initialized&&d.trigger("pagerChange",c);if(c.removeRows){k.clearTableBody(b);e=k.processTbody(b,e.$tbodies.eq(0),!0);f=m?0:n;g=m?0:n;for(l=0;l<r&&f<a.length;)m&&/filtered/.test(a[f][0].className)||(g++,g>n&&l<= r&&(l++,e.append(a[f]))),f++;k.processTbody(b,e,!1)}else v(b,c);t(b,c);c.isDisabled||u(b,c);d.trigger("applyWidgets");b.isUpdating&&d.trigger("updateComplete",b)}},D=function(b,a){a.ajax?p(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(),x(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})},q=function(b,a,c){if(!a.isDisabled){var e=b.config,f=h(b),g=a.last,l=Math.min(a.totalPages,a.filteredPages);0>a.page&&(a.page=0);a.page>l-1&&0!==l&&(a.page=l-1);g.currentFilters=""===(g.currentFilters||[]).join("")?[]:g.currentFilters;a.currentFilters=""===(a.currentFilters||[]).join("")?[]:a.currentFilters;if(g.page!==a.page||g.size!==a.size||g.totalRows!==a.totalRows||(g.currentFilters||[]).join(",")!==(a.currentFilters||[]).join(",")||g.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(","),totalRows:a.totalRows,currentFilters:a.currentFilters||[]},a.ajax?G(b,a):a.ajax||x(b,e.rowsCopy,a),h.data(b,"pagerLastPage",a.page),a.initialized&&!1!==c&&(f.trigger("pageMoved",a).trigger("applyWidgets"),b.isUpdating&&f.trigger("updateComplete"))}},y=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);c.filteredPages=Math.ceil(c.filteredRows/c.size);q(b,c)},H=function(b,a){a.page=0;q(b,a)},C=function(b,a){a.page=Math.min(a.totalPages,a.filteredPages)-1;q(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);q(b,a)},J=function(b,a){a.page--;0>=a.page&&(a.page=0);q(b,a)},E=function(b,a,c){var e,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&&(e=b.id+"_pager_info",a.$container.find(a.cssPageDisplay).attr("id",e),f.$table.attr("aria-describedby",e));c&&(f.$table.trigger("updateRows"),y(b,a.size,a),A(b,a),u(b,a),f.debug&&k.log("pager enabled"))};w.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),x(b,a,e),t(b,e,!1))};w.construct=function(b){return this.each(function(){if(this.config&&this.hasInitialized){var a,c,e,f=this,g=f.config,l=g.widgetOptions,d=g.pager=h.extend(!0,{},h.tablesorterPager.defaults,b),m=g.$table,n=d.$container=h(d.container).addClass("tablesorter-pager").show();g.debug&&k.log("Pager initializing"); d.oldAjaxSuccess=d.oldAjaxSuccess||d.ajaxObject.success;g.appender=w.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,d.storageKey)||{},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));d.regexRows=new RegExp("("+(l.filter_filteredRow||"filtered")+"|"+g.selectorRemove.substring(1)+ "|"+g.cssChildRow+")");m.unbind("filterStart filterEnd sortEnd disable enable destroy update updateRows updateAll addRows pageSize ".split(" ").join(".pager ")).bind("filterStart.pager",function(a,b){d.currentFilters=b;!1!==d.pageReset&&(g.lastCombinedFilter||"")!==(b||[]).join("")&&(d.page=d.pageReset)}).bind("filterEnd.pager sortEnd.pager",function(){d.initialized&&(t(f,d,!1),q(f,d,!1),u(f,d))}).bind("disable.pager",function(a){a.stopPropagation();D(f,d)}).bind("enable.pager",function(a){a.stopPropagation(); E(f,d,!0)}).bind("destroy.pager",function(a){a.stopPropagation();D(f,d);d.$container.hide();f.config.appender=null;d.initialized=!1;delete f.config.rowsCopy;h(f).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager");k.storage&&k.storage(f,d.storageKey,"")}).bind("update.pager updateRows.pager updateAll.pager addRows.pager ",function(a){a.stopPropagation();u(f,d);a=g.$tbodies.eq(0).children();d.totalRows=a.length-(d.countChildRows?0:a.filter("."+g.cssChildRow).length);d.totalPages= Math.ceil(d.totalRows/d.size);t(f,d);v(f,d)}).bind("pageSize.pager",function(a,b){a.stopPropagation();y(f,parseInt(b,10)||10,d);v(f,d);t(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);q(f,d);t(f,d,!1)});c=[d.cssFirst,d.cssPrev,d.cssNext,d.cssLast];e=[H,J,I,C];n.find(c.join(",")).attr("tabindex",0).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=n.find(d.cssGoto);d.$goto.length&&d.$goto.unbind("change").bind("change",function(){d.page=h(this).val()-1;q(f,d);t(f,d,!1)});d.$size=n.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)||(y(f,parseInt(h(this).val(),10),d),z(f,d));return!1});d.initialized=!1;m.trigger("pagerBeforeInitialized",d); E(f,d,!1);"string"===typeof d.ajaxUrl?(d.ajax=!0,g.widgetOptions.filter_serversideFiltering=!0,g.serverSideSorting=!0,q(f,d)):(d.ajax=!1,h(this).trigger("appendCache",!0),A(f,d));z(f,d);d.ajax||(d.initialized=!0,h(f).trigger("pagerInitialized",d))}})}}});k.showError=function(k,p){h(k).each(function(){var k=this.config,u=k.pager&&k.pager.cssErrorRow||k.widgetOptions.pager_css&&k.widgetOptions.pager_css.errorRow||"tablesorter-errorRow";k&&("undefined"===typeof p?k.$table.find("thead").find(k.selectorRemove).remove(): (/tr\>/.test(p)?h(p):h('<tr><td colspan="'+k.columns+'">'+p+"</td></tr>")).click(function(){h(this).remove()}).appendTo(k.$table.find("thead:first")).addClass(u+" "+k.selectorRemove.replace(/^[.#]/,"")).attr({role:"alert","aria-live":"assertive"}))})};h.fn.extend({tablesorterPager:h.tablesorterPager.construct})})(jQuery);
|