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 7/4/2014 (v2.17.4) */
|
|
;(function(h){var k=h.tablesorter;h.extend({tablesorterPager:new function(){this.defaults={container:null,ajaxUrl:null,customAjaxUrl:function(c,a){return a},ajaxObject:{dataType:"json"},processAjaxOnInit:!0,ajaxProcessing:function(c){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(c,a){var b=c.cssDisabled,e=!!a,g=e||0===c.page,f=Math.min(c.totalPages,c.filteredPages),e=e||c.page===f-1||0===f;c.updateArrows&&(c.$container.find(c.cssFirst+","+c.cssPrev)[g?"addClass":"removeClass"](b).attr("aria-disabled", g),c.$container.find(c.cssNext+","+c.cssLast)[e?"addClass":"removeClass"](b).attr("aria-disabled",e))},t=function(c,a,b){var e,g,f,l=c.config;e=l.$table.hasClass("hasFilters");f=[];g=a.size||10;f=[l.widgetOptions&&l.widgetOptions.filter_filteredRow||"filtered",l.selectorRemove.replace(/^(\w+\.)/g,"")];a.countChildRows&&f.push(l.cssChildRow);f.join("|");a.totalPages=Math.ceil(a.totalRows/g);e&&!a.ajaxUrl?(a.filteredRows=0,h.each(l.cache[0].normalized,function(d,b){a.filteredRows+=a.regexRows.test(b[l.columns].$row[0].className)? 0:1})):e||(a.filteredRows=a.totalRows);l.totalRows=a.totalRows;l.filteredRows=a.filteredRows;a.filteredPages=Math.ceil(a.filteredRows/g)||0;if(0<=Math.min(a.totalPages,a.filteredPages)&&(f=a.size*a.page>a.filteredRows,a.startRow=f?1:0===a.filteredRows?0:a.size*a.page+1,a.page=f?0:a.page,a.endRow=Math.min(a.filteredRows,a.totalRows,a.size*(a.page+1)),e=a.$container.find(a.cssPageDisplay),f=(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"](f),a.$goto.length))){f="";g=Math.min(a.totalPages,a.filteredPages);for(e=1;e<=g;e++)f+="<option>"+e+"</option>";a.$goto.html(f).val(a.page+1)}p(a);a.initialized&&!1!==b&&(l.$table.trigger("pagerComplete", a),a.savePages&&k.storage&&k.storage(c,a.storageKey,{page:a.page,size:a.size}))},u=function(c,a){var b,e=c.config,g=e.$tbodies.eq(0);a.fixedHeight&&(g.find("tr.pagerSavedHeightSpacer").remove(),b=h.data(c,"pagerSavedHeight"))&&(b-=g.height(),5<b&&h.data(c,"pagerLastSize")===a.size&&g.children("tr:visible").length<a.size&&g.append('<tr class="pagerSavedHeightSpacer '+e.selectorRemove.replace(/^(\w+\.)/g,"")+'" style="height:'+b+'px;"></tr>'))},z=function(c,a){var b=c.config.$tbodies.eq(0);b.find("tr.pagerSavedHeightSpacer").remove(); h.data(c,"pagerSavedHeight",b.height());u(c,a);h.data(c,"pagerLastSize",a.size)},v=function(c,a){if(!a.ajaxUrl){var b,e=0,g=c.config,f=g.$tbodies.eq(0).children("tr"),h=f.length,d=a.page*a.size,m=d+a.size,n=g.widgetOptions&&g.widgetOptions.filter_filteredRow||"filtered",s=0;for(b=0;b<h;b++)f[b].className.match(n)||(s===d&&f[b].className.match(g.cssChildRow)?f[b].style.display="none":(f[b].style.display=s>=d&&s<m?"":"none",s+=f[b].className.match(g.cssChildRow+"|"+g.selectorRemove.replace(/^(\w+\.)/g, ""))&&!a.countChildRows?0:1,s===m&&"none"!==f[b].style.display&&f[b].className.match(k.css.cssHasChild)&&(e=b)));if(0<e&&f[e].className.match(k.css.cssHasChild))for(;++e<h&&f[e].className.match(g.cssChildRow);)f[e].style.display=""}},A=function(c,a){a.size=parseInt(a.$size.val(),10)||a.size;h.data(c,"pagerLastSize",a.size);p(a);a.removeRows||(v(c,a),h(c).bind("sortEnd.pager filterEnd.pager",function(){v(c,a)}))},B=function(c,a,b,e,g){if("function"===typeof b.ajaxProcessing){var f,l,d,m,n,s,r=a.config, q=r.$table,p="";f=b.ajaxProcessing(c,a,e)||[0,[]];c=q.find("thead th").length;k.showError(a);if(g)r.debug&&k.log("Ajax Error",e,g),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"===g?"Requested JSON parse failed":"timeout"===g?"Time out error":"abort"===g?"Ajax Request aborted":"Uncaught error: "+e.statusText+" ["+e.status+"]"),r.$tbodies.eq(0).children().detach(),b.totalRows=0;else{h.isArray(f)? (e=isNaN(f[0])&&!isNaN(f[1]),g=f[e?1:0],b.totalRows=isNaN(g)?b.totalRows||0:g,r.totalRows=r.filteredRows=b.filteredRows=b.totalRows,e=0===b.totalRows?[""]:f[e?0:1]||[],n=f[2]):(b.ajaxData=f,r.totalRows=b.totalRows=f.total,r.filteredRows=b.filteredRows="undefined"!==typeof f.filteredRows?f.filteredRows:f.total,n=f.headers,e=f.rows);s=e&&e.length;if(e instanceof jQuery)b.processAjaxOnInit&&(r.$tbodies.eq(0).children().detach(),r.$tbodies.eq(0).append(e));else if(s){for(f=0;f<s;f++){p+="<tr>";for(g= 0;g<e[f].length;g++)p+=/^\s*<td/.test(e[f][g])?h.trim(e[f][g]):"<td>"+e[f][g]+"</td>";p+="</tr>"}b.processAjaxOnInit&&r.$tbodies.eq(0).html(p)}b.processAjaxOnInit=!0;n&&n.length===c&&(m=(l=q.hasClass("hasStickyHeaders"))?r.widgetOptions.$sticky.children("thead:first").children("tr").children():"",d=q.find("tfoot tr:first").children(),r.$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])}))}r.showProcessing&&k.isProcessing(a);b.totalPages=Math.ceil(b.totalRows/(b.size||10));b.last.totalRows=b.totalRows;b.last.currentFilters=b.currentFilters;b.last.sortList=(r.sortList||[]).join(",");t(a,b);u(a,b);q.trigger("updateCache",[function(){b.initialized&& setTimeout(function(){q.trigger("applyWidgets").trigger("pagerChange",b)},0)}])}b.initialized||(b.initialized=!0,h(a).trigger("applyWidgets").trigger("pagerInitialized",b))},G=function(c,a){var b=F(c,a),e=h(document),g,f=c.config;""!==b&&(f.showProcessing&&k.isProcessing(c,!0),e.bind("ajaxError.pager",function(b,d,g,f){B(null,c,a,d,f);e.unbind("ajaxError.pager")}),g=++a.ajaxCounter,a.ajaxObject.url=b,a.ajaxObject.success=function(b,d,f){g<a.ajaxCounter||(B(b,c,a,f),e.unbind("ajaxError.pager"),"function"=== typeof a.oldAjaxSuccess&&a.oldAjaxSuccess(b))},f.debug&&k.log("ajax initialized",a.ajaxObject),h.ajax(a.ajaxObject))},F=function(c,a){var b=c.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):"",g=b.sortList,f=a.currentFilters||h(c).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(g,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(f,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=f);"function"===typeof a.customAjaxUrl&&(e=a.customAjaxUrl(c,e));b.debug&&k.log("Pager ajax url: "+e);return e},x=function(c,a,b){var e,g,f,l,d=h(c);e=c.config;var m=e.$table.hasClass("hasFilters"),n=b.page*b.size,s=b.size;if(!(1>(a&& a.length||0))){b.page>=b.totalPages&&C(c,b);b.isDisabled=!1;b.initialized&&d.trigger("pagerChange",b);if(b.removeRows){k.clearTableBody(c);e=k.processTbody(c,e.$tbodies.eq(0),!0);g=m?0:n;f=m?0:n;for(l=0;l<s&&g<a.length;)m&&/filtered/.test(a[g][0].className)||(f++,f>n&&l<=s&&(l++,e.append(a[g]))),g++;k.processTbody(c,e,!1)}else v(c,b);t(c,b);b.isDisabled||u(c,b);d.trigger("applyWidgets");c.isUpdating&&d.trigger("updateComplete",c)}},D=function(c,a){a.ajax?p(a,!0):(a.isDisabled=!0,h.data(c,"pagerLastPage", a.page),h.data(c,"pagerLastSize",a.size),a.page=0,a.size=a.totalRows,a.totalPages=1,h(c).addClass("pagerDisabled").removeAttr("aria-describedby").find("tr.pagerSavedHeightSpacer").remove(),x(c,c.config.rowsCopy,a),c.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(c,a,b){if(!a.isDisabled){var e=c.config,g=h(c),f=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);f.currentFilters=""===(f.currentFilters||[]).join("")?[]:f.currentFilters;a.currentFilters=""===(a.currentFilters||[]).join("")?[]:a.currentFilters;if(f.page!==a.page||f.size!==a.size||f.totalRows!==a.totalRows||(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(","),totalRows:a.totalRows,currentFilters:a.currentFilters|| []},a.ajax?G(c,a):a.ajax||x(c,e.rowsCopy,a),h.data(c,"pagerLastPage",a.page),a.initialized&&!1!==b&&(g.trigger("pageMoved",a).trigger("applyWidgets"),c.isUpdating&&g.trigger("updateComplete"))}},y=function(c,a,b){b.size=a||b.size||10;b.$size.val(b.size);h.data(c,"pagerLastPage",b.page);h.data(c,"pagerLastSize",b.size);b.totalPages=Math.ceil(b.totalRows/b.size);b.filteredPages=Math.ceil(b.filteredRows/b.size);q(c,b)},H=function(c,a){a.page=0;q(c,a)},C=function(c,a){a.page=Math.min(a.totalPages,a.filteredPages)- 1;q(c,a)},I=function(c,a){a.page++;a.page>=Math.min(a.totalPages,a.filteredPages)-1&&(a.page=Math.min(a.totalPages,a.filteredPages)-1);q(c,a)},J=function(c,a){a.page--;0>=a.page&&(a.page=0);q(c,a)},E=function(c,a,b){var e,g=c.config;a.$size.add(a.$goto).removeClass(a.cssDisabled).removeAttr("disabled").attr("aria-disabled","false");a.isDisabled=!1;a.page=h.data(c,"pagerLastPage")||a.page||0;a.size=h.data(c,"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);c.id&&(e=c.id+"_pager_info",a.$container.find(a.cssPageDisplay).attr("id",e),g.$table.attr("aria-describedby",e));b&&(g.$table.trigger("updateRows"),y(c,a.size,a),A(c,a),u(c,a),g.debug&&k.log("pager enabled"))};w.appender=function(c,a){var b=c.config,e=b.pager;e.ajax||(b.rowsCopy=a,e.totalRows=e.countChildRows?b.$tbodies.eq(0).children("tr").length:a.length,e.size=h.data(c,"pagerLastSize")||e.size||10,e.totalPages=Math.ceil(e.totalRows/ e.size),x(c,a,e),t(c,e,!1))};w.construct=function(c){return this.each(function(){if(this.config&&this.hasInitialized){var a,b,e,g=this,f=g.config,l=f.widgetOptions,d=f.pager=h.extend(!0,{},h.tablesorterPager.defaults,c),m=f.$table,n=d.$container=h(d.container).addClass("tablesorter-pager").show();f.debug&&k.log("Pager initializing");d.oldAjaxSuccess=d.oldAjaxSuccess||d.ajaxObject.success;f.appender=w.appender;k.filter&&0<=h.inArray("filter",f.widgets)&&(d.currentFilters=f.$table.data("lastSearch")|| k.filter.setDefaults(g,f,f.widgetOptions)||[],k.setFilters(g,d.currentFilters,!1));d.savePages&&k.storage&&(a=k.storage(g,d.storageKey)||{},d.page=isNaN(a.page)?d.page:a.page,d.size=(isNaN(a.size)?d.size:a.size)||10,h.data(g,"pagerLastSize",d.size));d.regexRows=new RegExp("("+(l.filter_filteredRow||"filtered")+"|"+f.selectorRemove.replace(/^(\w+\.)/g,"")+"|"+f.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&&(f.lastCombinedFilter||"")!==(b||[]).join("")&&(d.page=d.pageReset)}).bind("filterEnd.pager sortEnd.pager",function(){d.initialized&&(t(g,d,!1),q(g,d,!1),u(g,d))}).bind("disable.pager",function(a){a.stopPropagation();D(g,d)}).bind("enable.pager",function(a){a.stopPropagation();E(g,d,!0)}).bind("destroy.pager",function(a){a.stopPropagation();D(g,d);d.$container.hide();g.config.appender=null;d.initialized=!1;delete g.config.rowsCopy;h(g).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager"); k.storage&&k.storage(g,d.storageKey,"")}).bind("update.pager updateRows.pager updateAll.pager addRows.pager ",function(a){a.stopPropagation();u(g,d);a=f.$tbodies.eq(0).children("tr");d.totalRows=a.length-(d.countChildRows?0:a.filter("."+f.cssChildRow).length);d.totalPages=Math.ceil(d.totalRows/d.size);t(g,d);v(g,d)}).bind("pageSize.pager",function(a,b){a.stopPropagation();y(g,parseInt(b,10)||10,d);v(g,d);t(g,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(g,d);t(g,d,!1)});b=[d.cssFirst,d.cssPrev,d.cssNext,d.cssLast];e=[H,J,I,C];n.find(b.join(",")).attr("tabindex",0).unbind("click.pager").bind("click.pager",function(a){a.stopPropagation();var c=h(this),f=b.length;if(!c.hasClass(d.cssDisabled))for(a=0;a<f;a++)if(c.is(b[a])){e[a](g,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(g,d);t(g,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(g,parseInt(h(this).val(),10),d),z(g,d));return!1});d.initialized=!1;m.trigger("pagerBeforeInitialized",d);E(g,d,!1);"string"===typeof d.ajaxUrl?(d.ajax=!0,f.widgetOptions.filter_serversideFiltering=!0,f.serverSideSorting=!0,q(g,d)):(d.ajax=!1,h(this).trigger("appendCache",!0),A(g,d));z(g,d);d.ajax||(d.initialized=!0,h(g).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(/^(\w+\.)/g,"")).attr({role:"alert","aria-live":"assertive"}))})}; h.fn.extend({tablesorterPager:h.tablesorterPager.construct})})(jQuery);
|