From 44f6a4771f0d37f09bdcf28ec21caa63d8eb1c8e Mon Sep 17 00:00:00 2001 From: Joel Perry Date: Fri, 9 Sep 2016 09:29:50 +0100 Subject: [PATCH] Allow for optional output callback in favour of parsable string --- addons/pager/jquery.tablesorter.pager.js | 48 +++++++++++++----------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/addons/pager/jquery.tablesorter.pager.js b/addons/pager/jquery.tablesorter.pager.js index 55a778b0..c4a7f47b 100644 --- a/addons/pager/jquery.tablesorter.pager.js +++ b/addons/pager/jquery.tablesorter.pager.js @@ -182,27 +182,33 @@ p.startRow = (t) ? sz * p.page + 1 : (p.filteredRows === 0 ? 0 : sz * p.page + 1); p.endRow = Math.min( p.filteredRows, p.totalRows, sz * ( p.page + 1 ) ); $out = p.$container.find(p.cssPageDisplay); - // form the output string (can now get a new output string from the server) - s = ( p.ajaxData && p.ajaxData.output ? p.ajaxData.output || p.output : p.output ) - // {page} = one-based index; {page+#} = zero based index +/- value - .replace(/\{page([\-+]\d+)?\}/gi, function(m, n){ - return p.totalPages ? p.page + (n ? parseInt(n, 10) : 1) : 0; - }) - // {totalPages}, {extra}, {extra:0} (array) or {extra : key} (object) - .replace(/\{\w+(\s*:\s*\w+)?\}/gi, function(m){ - var len, indx, - str = m.replace(/[{}\s]/g, ''), - extra = str.split(':'), - data = p.ajaxData, - // return zero for default page/row numbers - deflt = /(rows?|pages?)$/i.test(str) ? 0 : ''; - if (/(startRow|page)/.test(extra[0]) && extra[1] === 'input') { - len = ('' + (extra[0] === 'page' ? p.totalPages : p.totalRows)).length; - indx = extra[0] === 'page' ? p.page + 1 : p.startRow; - return ''; - } - return extra.length > 1 && data && data[extra[0]] ? data[extra[0]][extra[1]] : p[str] || (data ? data[str] : deflt) || deflt; - }); + + // Output param can be callback for custom rendering or string + if (typeof p.output === 'function') { + s = p.output(table, p); + } else { + // form the output string (can now get a new output string from the server) + s = ( p.ajaxData && p.ajaxData.output ? p.ajaxData.output || p.output : p.output ) + // {page} = one-based index; {page+#} = zero based index +/- value + .replace(/\{page([\-+]\d+)?\}/gi, function(m, n){ + return p.totalPages ? p.page + (n ? parseInt(n, 10) : 1) : 0; + }) + // {totalPages}, {extra}, {extra:0} (array) or {extra : key} (object) + .replace(/\{\w+(\s*:\s*\w+)?\}/gi, function(m){ + var len, indx, + str = m.replace(/[{}\s]/g, ''), + extra = str.split(':'), + data = p.ajaxData, + // return zero for default page/row numbers + deflt = /(rows?|pages?)$/i.test(str) ? 0 : ''; + if (/(startRow|page)/.test(extra[0]) && extra[1] === 'input') { + len = ('' + (extra[0] === 'page' ? p.totalPages : p.totalRows)).length; + indx = extra[0] === 'page' ? p.page + 1 : p.startRow; + return ''; + } + return extra.length > 1 && data && data[extra[0]] ? data[extra[0]][extra[1]] : p[str] || (data ? data[str] : deflt) || deflt; + }); + } if ( p.$goto.length ) { t = ''; options = buildPageSelect( table, p );