From baf95b134035fdee43c351bd36e21c9f242e8c97 Mon Sep 17 00:00:00 2001 From: Owen Leonard Date: Fri, 18 Jan 2013 15:55:47 -0500 Subject: [PATCH] Bug 9431 [Revised] Use DataTables on Patron Category Administration page Replace the tablesorter plugin with the DataTables plugin on the patron category administration page. Structural changes were made to the table markup for validity and to deal with a DataTables bug which prevents it from properly parsing a with a colspan. To test, open the Patron Category Administration page (Administration -> Patron types and categories). Confirm that table sorting works correctly. Revision adds a "natural sort" plugin to the main datatables configuration JavaScript file to enable sorting of columns like those in this table which include mixed numeric and text data. Allows correct numeric sorting of data like "0 years, 1 years, 2 years, 18 years," etc. Further revision corrects template path to datatables assets. Signed-off-by: Bernardo Gonzalez Kriegel Comment: Sorting works. No errors. Signed-off-by: Jonathan Druart Signed-off-by: Jared Camins-Esakov --- .../intranet-tmpl/prog/en/js/datatables.js | 57 +++++++++++++++++++ .../prog/en/modules/admin/categorie.tt | 39 ++++++++----- 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js index b43f2f8c17..b350211fbe 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js @@ -414,3 +414,60 @@ $.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) { y = parseFloat( y ); return ((x < y) ? 1 : ((x > y) ? -1 : 0)); }; + +(function() { + +/* + * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license + * Author: Jim Palmer (based on chunking idea from Dave Koelle) + * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo + * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js + */ +function naturalSort (a, b) { + var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, + sre = /(^[ ]*|[ ]*$)/g, + dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, + hre = /^0x[0-9a-f]+$/i, + ore = /^0/, + // convert all to strings and trim() + x = a.toString().replace(sre, '') || '', + y = b.toString().replace(sre, '') || '', + // chunk/tokenize + xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), + // numeric, hex or date detection + xD = parseInt(x.match(hre), 10) || (xN.length != 1 && x.match(dre) && Date.parse(x)), + yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null; + // first try and sort Hex codes or Dates + if (yD) + if ( xD < yD ) return -1; + else if ( xD > yD ) return 1; + // natural sorting through split numeric strings and default strings + for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { + // find floats not starting with '0', string or 0 if not defined (Clint Priest) + var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0; + var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0; + // handle numeric vs string comparison - number < string - (Kyle Adams) + if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1; + // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' + else if (typeof oFxNcL !== typeof oFyNcL) { + oFxNcL += ''; + oFyNcL += ''; + } + if (oFxNcL < oFyNcL) return -1; + if (oFxNcL > oFyNcL) return 1; + } + return 0; +} + +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "natural-asc": function ( a, b ) { + return naturalSort(a,b); + }, + + "natural-desc": function ( a, b ) { + return naturalSort(a,b) * -1; + } +} ); + +}()); \ No newline at end of file diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt index e7e00d4ee0..ad573152f9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt @@ -5,23 +5,30 @@ [% IF ( delete_confirmed ) %]Category deleted[% END %] [% INCLUDE 'doc-head-close.inc' %] [% INCLUDE 'calendar.inc' %] - - - +[% INCLUDE 'datatables-strings.inc' %] + + -