From 409edd791b195c3b773aa012af4cd4138f9212b4 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 17 Feb 2012 09:29:41 +0000 Subject: [PATCH] Fix: When sorting non-string data as a string (type to type detection) then DataTables 1.9 would automatically convert the non-string data to an empty string so it can be sorted. This can result in unexpected ordering in the table. The fix is to check for a toString() function that is available for the data and if it is there, then use it (great for numeric data, dates etc), otherwise the empty string is used (null, methods without toString). This brings 1.9 back into line with how 1.8 behaved - 8549 --- media/js/jquery.dataTables.js | 4 +- media/src/ext/ext.sorting.js | 4 +- .../2_js/8549--string-sorting-nonstrings.js | 47 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 media/unit_testing/tests_onhold/2_js/8549--string-sorting-nonstrings.js diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 3795f1b5..9c64c8b3 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -11337,7 +11337,9 @@ */ "string-pre": function ( a ) { - if ( typeof a != 'string' ) { a = ''; } + if ( typeof a != 'string' ) { + a = (a !== null && a.toString) ? a.toString() : ''; + } return a.toLowerCase(); }, diff --git a/media/src/ext/ext.sorting.js b/media/src/ext/ext.sorting.js index 44382579..93ab015b 100644 --- a/media/src/ext/ext.sorting.js +++ b/media/src/ext/ext.sorting.js @@ -5,7 +5,9 @@ $.extend( DataTable.ext.oSort, { */ "string-pre": function ( a ) { - if ( typeof a != 'string' ) { a = ''; } + if ( typeof a != 'string' ) { + a = (a !== null && a.toString) ? a.toString() : ''; + } return a.toLowerCase(); }, diff --git a/media/unit_testing/tests_onhold/2_js/8549--string-sorting-nonstrings.js b/media/unit_testing/tests_onhold/2_js/8549--string-sorting-nonstrings.js new file mode 100644 index 00000000..57ffebc0 --- /dev/null +++ b/media/unit_testing/tests_onhold/2_js/8549--string-sorting-nonstrings.js @@ -0,0 +1,47 @@ +// DATA_TEMPLATE: empty_table +oTest.fnStart( "8549 - string sorting non-string types" ); + +$(document).ready( function () { + var test = false; + + $.fn.dataTable.ext.sErrMode = "throw"; + + + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Shallow properties + */ + + $('#example').dataTable( { + "aaData": [ + [ null ], + [ 5 ], + [ "1a" ], + [ new Date(0) ] + ], + "aoColumns": [ + { "sTitle": "Test" } + ] + } ); + + oTest.fnTest( + "Sorting works - first cell is empty", + null, + function () { return $('#example tbody tr:eq(0) td:eq(0)').html() === ""; } + ); + + oTest.fnTest( + "Second cell is 1a", + null, + function () { return $('#example tbody tr:eq(1) td:eq(0)').html() === "1a"; } + ); + + oTest.fnTest( + "Third cell is 5", + null, + function () { return $('#example tbody tr:eq(2) td:eq(0)').html() === "5"; } + ); + + + oTest.fnComplete(); +} ); \ No newline at end of file