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