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 8/1/2014 (v2.17.6) */
|
|
;(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,e=!!a,g=e||0===b.page,f=Math.min(b.totalPages,b.filteredPages),e=e||b.page===f-1||0===f;b.updateArrows&&(b.$container.find(b.cssFirst+","+b.cssPrev)[g?"addClass":"removeClass"](d).attr("aria-disabled", g),b.$container.find(b.cssNext+","+b.cssLast)[e?"addClass":"removeClass"](d).attr("aria-disabled",e))},t=function(b,a,d){var e,g,f,l,c=b.config;l=c.$table.hasClass("hasFilters");f=[];e=a.size||10;f=[c.widgetOptions&&c.widgetOptions.filter_filteredRow||"filtered",c.selectorRemove.replace(/^(\w+\.)/g,"")];a.countChildRows&&f.push(c.cssChildRow);f.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(d,b){a.filteredRows+=a.regexRows.test(b[c.columns].$row[0].className)?0:1})):l||(a.filteredRows=a.totalRows);a.totalPages=Math.ceil(a.totalRows/e);c.totalRows=a.totalRows;c.filteredRows=a.filteredRows;a.filteredPages=Math.ceil(a.filteredRows/e)||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)),l=a.$container.find(a.cssPageDisplay),f=(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"](f);if(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)}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,e=b.config,g=e.$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 '+e.selectorRemove.replace(/^(\w+\.)/g,"")+'" style="height:'+d+'px;"></tr>'))}, z=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,e=0,g=b.config,f=g.$tbodies.eq(0).children("tr"),h=f.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++)f[d].className.match(n)||(s===c&&f[d].className.match(g.cssChildRow)?f[d].style.display="none":(f[d].style.display=s>=c&& s<m?"":"none",s+=f[d].className.match(g.cssChildRow+"|"+g.selectorRemove.replace(/^(\w+\.)/g,""))&&!a.countChildRows?0:1,s===m&&"none"!==f[d].style.display&&f[d].className.match(k.css.cssHasChild)&&(e=d)));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(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,d,e,g){if("function"===typeof d.ajaxProcessing){var f,l,c,m,n,s,r=a.config,q=r.$table,p="";f=d.ajaxProcessing(b,a,e)||[0,[]];b=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("tr").detach(),d.totalRows=0;else{h.isArray(f)?(e=isNaN(f[0])&&!isNaN(f[1]),g=f[e?1:0],d.totalRows=isNaN(g)?d.totalRows||0:g,r.totalRows=r.filteredRows=d.filteredRows=d.totalRows,e=0===d.totalRows?[""]:f[e?0:1]||[],n=f[2]):(d.ajaxData=f,r.totalRows=d.totalRows=f.total,r.filteredRows=d.filteredRows="undefined"!==typeof f.filteredRows?f.filteredRows:f.total,n=f.headers,e=f.rows);s=e&&e.length;if(e instanceof jQuery)d.processAjaxOnInit&&(r.$tbodies.eq(0).children("tr").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>"}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),e=h(document),g,f=b.config;""!==d&&(f.showProcessing&&k.isProcessing(b,!0),e.bind("ajaxError.pager",function(d,c,f,g){B(null,b,a,c,g);e.unbind("ajaxError.pager")}),g=++a.ajaxCounter,a.ajaxObject.url=d,a.ajaxObject.success= function(d,c,f){g<a.ajaxCounter||(B(d,b,a,f),e.unbind("ajaxError.pager"),"function"===typeof a.oldAjaxSuccess&&a.oldAjaxSuccess(d))},f.debug&&k.log("ajax initialized",a.ajaxObject),h.ajax(a.ajaxObject))},G=function(b,a){var d=b.config,e=a.ajaxUrl?a.ajaxUrl.replace(/\{page([\-+]\d+)?\}/,function(c,b){return a.page+(b?parseInt(b,10):0)}).replace(/\{size\}/g,a.size):"",g=d.sortList,f=a.currentFilters||h(b).data("lastSearch")||[],l=e.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),c=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=[]);c&&(c=c[1],h.each(f,function(a,b){b&&m.push(c+"["+a+"]="+encodeURIComponent(b))}),e=e.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g,m.length?m.join("&"):c),a.currentFilters=f);"function"===typeof a.customAjaxUrl&&(e=a.customAjaxUrl(b,e));d.debug&&k.log("Pager ajax url: "+e);return e},x=function(b,a,d){var e,g,f,l,c=h(b);e=b.config;var m=e.$table.hasClass("hasFilters"), n=d.page*d.size,s=d.size;if(1>(a&&a.length||0))e.debug&&k.log("Pager: no rows for pager to render");else{d.page>=d.totalPages&&C(b,d);d.isDisabled=!1;d.initialized&&c.trigger("pagerChange",d);if(d.removeRows){k.clearTableBody(b);e=k.processTbody(b,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(b,e,!1)}else v(b,d);t(b,d);d.isDisabled||u(b,d);c.trigger("applyWidgets");b.isUpdating&&c.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).add(a.$container.find(".ts-startRow, .ts-page")).each(function(){h(this).attr("aria-disabled","true").addClass(a.cssDisabled)[0].disabled=!0})},E=function(b){var a= b.config,d=a.pager;a.$table.trigger("updateCache",[function(){var e,g=[],f=b.config.cache[0].normalized;d.totalRows=f.length;for(e=0;e<d.totalRows;e++)g.push(f[e][a.columns].$row);a.rowsCopy=g;q(b,d,!0)}])},q=function(b,a,d){if(!a.isDisabled){var e=b.config,g=h(b),f=a.last,l=Math.min(a.totalPages,a.filteredPages);if(!1!==d&&a.initialized&&h.isEmptyObject(b.config.cache))return E(b);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?H(b,a):a.ajax||x(b,e.rowsCopy,a),h.data(b,"pagerLastPage",a.page),a.initialized&& !1!==d&&(g.trigger("pageMoved",a).trigger("applyWidgets"),b.isUpdating&&g.trigger("updateComplete"))}},y=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)},C=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 e,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&&(e=b.id+"_pager_info",a.$container.find(a.cssPageDisplay).attr("id",e),g.$table.attr("aria-describedby",e));d&&(g.$table.trigger("updateRows"),y(b,a.size,a),A(b,a),u(b,a),g.debug&&k.log("pager enabled"))};w.appender=function(b,a){var d=b.config,e=d.pager;e.ajax||(d.rowsCopy=a,e.totalRows=e.countChildRows?d.$tbodies.eq(0).children("tr").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,d,e,g=this,f=g.config,l=f.widgetOptions,c=f.pager=h.extend(!0,{},h.tablesorterPager.defaults,b),m=f.$table,n=c.$container=h(c.container).addClass("tablesorter-pager").show();f.debug&&k.log("Pager initializing");c.oldAjaxSuccess=c.oldAjaxSuccess||c.ajaxObject.success;f.appender=w.appender;k.filter&&0<=h.inArray("filter",f.widgets)&&(c.currentFilters=f.$table.data("lastSearch")||k.filter.setDefaults(g,f,f.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")+"|"+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){c.currentFilters=b;!1!==c.pageReset&&(f.lastCombinedFilter||"")!==(b||[]).join("")&&(c.page=c.pageReset)}).bind("filterEnd.pager sortEnd.pager",function(){c.initialized&&(f.delayInit&&f.rowsCopy&&0===f.rowsCopy.length&&E(g),t(g,c,!1),q(g,c,!1),u(g,c))}).bind("disable.pager",function(a){a.stopPropagation();D(g,c)}).bind("enable.pager",function(a){a.stopPropagation();F(g,c,!0)}).bind("destroy.pager",function(a){a.stopPropagation();D(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("update.pager updateRows.pager updateAll.pager addRows.pager ",function(a){a.stopPropagation();u(g,c);a=f.$tbodies.eq(0).children("tr");c.totalRows=a.length-(c.countChildRows?0:a.filter("."+f.cssChildRow).length);c.totalPages=Math.ceil(c.totalRows/c.size);t(g,c);v(g,c)}).bind("pageSize.pager",function(a,b){a.stopPropagation();y(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];e=[I,K,J,C];n.find(d.join(",")).attr("tabindex",0).unbind("click.pager").bind("click.pager",function(a){a.stopPropagation();var b=h(this),f=d.length;if(!b.hasClass(c.cssDisabled))for(a=0;a<f;a++)if(b.is(d[a])){e[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)||(y(g,parseInt(h(this).val(),10),c),z(g,c));return!1}));c.initialized=!1;m.trigger("pagerBeforeInitialized",c);F(g,c,!1);"string"===typeof c.ajaxUrl?(c.ajax=!0,f.widgetOptions.filter_serversideFiltering= !0,f.serverSideSorting=!0,q(g,c)):(c.ajax=!1,h(this).trigger("appendCache",!0),A(g,c));z(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);
|