mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
3 lines
15 KiB
JavaScript
3 lines
15 KiB
JavaScript
/* tablesorter pager plugin updated 7/17/2014 (v2.17.5) */
|
|
;(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,d=!!a,g=d||0===c.page,f=Math.min(c.totalPages,c.filteredPages),d=d||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)[d?"addClass":"removeClass"](b).attr("aria-disabled",d))},t=function(c,a,b){var d,g,f,l=c.config;d=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("|");d&&!a.ajaxUrl?h.isEmptyObject(l.cache)?a.filteredRows=a.totalRows=l.$tbodies.eq(0).children("tr").not(a.countChildRows?"":"."+l.cssChildRow).length: (a.filteredRows=0,h.each(l.cache[0].normalized,function(e,b){a.filteredRows+=a.regexRows.test(b[l.columns].$row[0].className)?0:1})):d||(a.filteredRows=a.totalRows);a.totalPages=Math.ceil(a.totalRows/g);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)),d=a.$container.find(a.cssPageDisplay),f=(a.ajaxData&&a.ajaxData.output?a.ajaxData.output||a.output:a.output).replace(/\{page([\-+]\d+)?\}/gi,function(e,b){return a.totalPages?a.page+(b?parseInt(b,10):1):0}).replace(/\{\w+(\s*:\s*\w+)?\}/gi,function(b){b=b.replace(/[{}\s]/g,"");var c=b.split(":"),d=a.ajaxData,g=/(rows?|pages?)$/i.test(b)?0:"";return 1<c.length&&d&&d[c[0]]?d[c[0]][c[1]]:a[b]||(d?d[b]:g)||g}),d.length&&(d["INPUT"===d[0].tagName?"val":"html"](f),a.$goto.length))){f="";g=Math.min(a.totalPages, a.filteredPages);for(d=1;d<=g;d++)f+="<option>"+d+"</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,d=c.config,g=d.$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 '+ d.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,d=0,g=c.config,f=g.$tbodies.eq(0).children("tr"),h=f.length,e=a.page*a.size,m=e+a.size,n=g.widgetOptions&&g.widgetOptions.filter_filteredRow||"filtered",s=0;for(b=0;b<h;b++)f[b].className.match(n)||(s===e&&f[b].className.match(g.cssChildRow)? f[b].style.display="none":(f[b].style.display=s>=e&&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)&&(d=b)));if(0<d&&f[d].className.match(k.css.cssHasChild))for(;++d<h&&f[d].className.match(g.cssChildRow);)f[d].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,d,g){if("function"===typeof b.ajaxProcessing){var f,l,e,m,n,s,r=a.config,q=r.$table,p="";f=b.ajaxProcessing(c,a,d)||[0,[]];c=q.find("thead th").length;k.showError(a);if(g)r.debug&&k.log("Ajax Error",d,g),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"===g?"Requested JSON parse failed":"timeout"===g?"Time out error":"abort"===g?"Ajax Request aborted": "Uncaught error: "+d.statusText+" ["+d.status+"]"),r.$tbodies.eq(0).children("tr").detach(),b.totalRows=0;else{h.isArray(f)?(d=isNaN(f[0])&&!isNaN(f[1]),g=f[d?1:0],b.totalRows=isNaN(g)?b.totalRows||0:g,r.totalRows=r.filteredRows=b.filteredRows=b.totalRows,d=0===b.totalRows?[""]:f[d?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,d=f.rows);s=d&&d.length;if(d instanceof jQuery)b.processAjaxOnInit&& (r.$tbodies.eq(0).children("tr").detach(),r.$tbodies.eq(0).append(d));else if(s){for(f=0;f<s;f++){p+="<tr>";for(g=0;g<d[f].length;g++)p+=/^\s*<td/.test(d[f][g])?h.trim(d[f][g]):"<td>"+d[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():"",e=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]));e.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))},H=function(c,a){var b=G(c,a),d=h(document),g,f=c.config;""!==b&&(f.showProcessing&&k.isProcessing(c,!0),d.bind("ajaxError.pager",function(b,e,g,f){B(null,c,a,e,f);d.unbind("ajaxError.pager")}),g=++a.ajaxCounter, a.ajaxObject.url=b,a.ajaxObject.success=function(b,e,f){g<a.ajaxCounter||(B(b,c,a,f),d.unbind("ajaxError.pager"),"function"===typeof a.oldAjaxSuccess&&a.oldAjaxSuccess(b))},f.debug&&k.log("ajax initialized",a.ajaxObject),h.ajax(a.ajaxObject))},G=function(c,a){var b=c.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):"",g=b.sortList,f=a.currentFilters||h(c).data("lastSearch")||[],l=d.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/), e=d.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])}),d=d.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):l),m=[]);e&&(e=e[1],h.each(f,function(a,b){b&&m.push(e+"["+a+"]="+encodeURIComponent(b))}),d=d.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):e),a.currentFilters=f);"function"===typeof a.customAjaxUrl&&(d=a.customAjaxUrl(c,d));b.debug&&k.log("Pager ajax url: "+d);return d},x=function(c, a,b){var d,g,f,l,e=h(c);d=c.config;var m=d.$table.hasClass("hasFilters"),n=b.page*b.size,s=b.size;if(1>(a&&a.length||0))d.debug&&k.log("Pager: no rows for pager to render");else{b.page>=b.totalPages&&C(c,b);b.isDisabled=!1;b.initialized&&e.trigger("pagerChange",b);if(b.removeRows){k.clearTableBody(c);d=k.processTbody(c,d.$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++,d.append(a[g]))),g++;k.processTbody(c,d,!1)}else v(c,b);t(c, b);b.isDisabled||u(c,b);e.trigger("applyWidgets");c.isUpdating&&e.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})},E=function(c){var a=c.config,b=a.pager;a.$table.trigger("updateCache",[function(){var d,g=[],f=c.config.cache[0].normalized;b.totalRows=f.length;for(d=0;d<b.totalRows;d++)g.push(f[d][a.columns].$row);a.rowsCopy=g;q(c,b,!0)}])},q=function(c,a,b){if(!a.isDisabled){var d=c.config,g=h(c),f=a.last,l=Math.min(a.totalPages,a.filteredPages);if(!1!==b&&a.initialized&&h.isEmptyObject(c.config.cache))return E(c);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!==(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(","),totalRows:a.totalRows,currentFilters:a.currentFilters||[]},a.ajax?H(c,a):a.ajax||x(c,d.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)},I=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)},J=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)},K=function(c,a){a.page--;0>=a.page&&(a.page=0);q(c,a)},F=function(c,a,b){var d,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&&(d=c.id+"_pager_info",a.$container.find(a.cssPageDisplay).attr("id",d),g.$table.attr("aria-describedby",d));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,d=b.pager;d.ajax||(b.rowsCopy=a,d.totalRows=d.countChildRows?b.$tbodies.eq(0).children("tr").length:a.length,d.size=h.data(c,"pagerLastSize")||d.size||10,d.totalPages=Math.ceil(d.totalRows/d.size),x(c,a,d),t(c,d,!1))};w.construct= function(c){return this.each(function(){if(this.config&&this.hasInitialized){var a,b,d,g=this,f=g.config,l=f.widgetOptions,e=f.pager=h.extend(!0,{},h.tablesorterPager.defaults,c),m=f.$table,n=e.$container=h(e.container).addClass("tablesorter-pager").show();f.debug&&k.log("Pager initializing");e.oldAjaxSuccess=e.oldAjaxSuccess||e.ajaxObject.success;f.appender=w.appender;k.filter&&0<=h.inArray("filter",f.widgets)&&(e.currentFilters=f.$table.data("lastSearch")||k.filter.setDefaults(g,f,f.widgetOptions)|| [],k.setFilters(g,e.currentFilters,!1));e.savePages&&k.storage&&(a=k.storage(g,e.storageKey)||{},e.page=isNaN(a.page)?e.page:a.page,e.size=(isNaN(a.size)?e.size:a.size)||10,h.data(g,"pagerLastSize",e.size));e.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){e.currentFilters=b;!1!==e.pageReset&&(f.lastCombinedFilter||"")!==(b||[]).join("")&&(e.page=e.pageReset)}).bind("filterEnd.pager sortEnd.pager",function(){e.initialized&&(f.delayInit&&f.rowsCopy&&0===f.rowsCopy.length&&E(g),t(g,e,!1),q(g,e,!1),u(g,e))}).bind("disable.pager",function(a){a.stopPropagation();D(g,e)}).bind("enable.pager",function(a){a.stopPropagation();F(g,e,!0)}).bind("destroy.pager",function(a){a.stopPropagation();D(g,e);e.$container.hide();g.config.appender=null;e.initialized=!1; delete g.config.rowsCopy;h(g).unbind("destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager");k.storage&&k.storage(g,e.storageKey,"")}).bind("update.pager updateRows.pager updateAll.pager addRows.pager ",function(a){a.stopPropagation();u(g,e);a=f.$tbodies.eq(0).children("tr");e.totalRows=a.length-(e.countChildRows?0:a.filter("."+f.cssChildRow).length);e.totalPages=Math.ceil(e.totalRows/e.size);t(g,e);v(g,e)}).bind("pageSize.pager",function(a,b){a.stopPropagation();y(g,parseInt(b, 10)||10,e);v(g,e);t(g,e,!1);e.$size.length&&e.$size.val(e.size)}).bind("pageSet.pager",function(a,b){a.stopPropagation();e.page=(parseInt(b,10)||1)-1;e.$goto.length&&e.$goto.val(e.size);q(g,e,!0);t(g,e,!1)});b=[e.cssFirst,e.cssPrev,e.cssNext,e.cssLast];d=[I,K,J,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(e.cssDisabled))for(a=0;a<f;a++)if(c.is(b[a])){d[a](g,e);break}});e.$goto=n.find(e.cssGoto); e.$goto.length&&e.$goto.unbind("change").bind("change",function(){e.page=h(this).val()-1;q(g,e,!0);t(g,e,!1)});e.$size=n.find(e.cssPageSize);e.$size.length&&e.$size.unbind("change.pager").bind("change.pager",function(){e.$size.val(h(this).val());h(this).hasClass(e.cssDisabled)||(y(g,parseInt(h(this).val(),10),e),z(g,e));return!1});e.initialized=!1;m.trigger("pagerBeforeInitialized",e);F(g,e,!1);"string"===typeof e.ajaxUrl?(e.ajax=!0,f.widgetOptions.filter_serversideFiltering=!0,f.serverSideSorting= !0,q(g,e)):(e.ajax=!1,h(this).trigger("appendCache",!0),A(g,e));z(g,e);e.ajax||(e.initialized=!0,h(g).trigger("pagerInitialized",e))}})}}});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);
|