(Bug?) Javascript error on sort with booleans in column

(Bug?) Javascript error on sort with booleans in column

Jens GutzeitJens Gutzeit Posts: 22Questions: 0Answers: 0
edited January 2010 in General
Hello allan and others,

In the first place I'm not quite sure if it's a bug, or if it is my lack of providing a decent typecasting during JSON generation, in the second place (and this really iritates me) I do not understand why this part of the code is executed at all.

Ok, let's look at it:
Given a table with some columns, data is provided by JSON, null is "casted" to empty String objects, the first value of column XYZ is Boolen true, the others are String '' (but I assume this doesn't matter at all). Sort hits the server, a new JSON is fetched and processed if I click the columns TH, but before that "_oExt.oSort" is run (why?), that crashes with the following javascript error:

a.toLowerCase is not a function
anonymous(true, Object name=b)jquery.d...Tables.js (Zeile 591)
anonymous()jquery.d...Tables.js (Zeile 3470)
_fnSort(Object sInstance=my_datafox_person_Person, Object name=bApplyClasses)jquery.d...Tables.js (Zeile 3471)
anonymous()jquery.d...Tables.js (Zeile 2443)
anonymous()jquery.d...Tables.js (Zeile 2454)
[Break on this error] var x = a.toLowerCase();\njquery.d...Tables.js (Zeile 591)

It's line 591, because I added a bit debug code to track this down to the values in the affected variable:
[code]
"string-asc": function ( a, b )
{
console.log('Value a:' + a);
console.log('Value b:' + b);
var x = a.toLowerCase();
var y = b.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
},
[/code]

Output in my firefox console before the JS error:
Value a:true
Value b:

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Jens,

    Yes - I'd say that the sorting function being run is a bug. The reason it was being run is that the click event on the header was running _fnSort as normal. DataTables was then doing a sort on the data it had (but not displaying the result) - then going and doing the Server-side / Ajax refresh.

    In the function _fnSort, you'll find the line:

    [code]
    if ( oSettings.aaSorting.length !== 0 || oSettings.aaSortingFixed !== null )
    [/code]
    this should be changed to:

    [code]
    if ( !oSettings.oFeatures.bServerSide &&
    (oSettings.aaSorting.length !== 0 || oSettings.aaSortingFixed !== null) )
    [/code]
    And hopefully that will do the trick for you.

    The reason that the sorting function was running into problems is that toLowerCase() isn't a method for a Boolean variable type (at least that's my guess). So either a special sorting function would be needed to cope with booleans, or the string sort function modified. Hopefully with the above fix (which I'll release in 1.5.7 at some point) will mitigate the need for any other changes though.

    Regards,
    Allan
  • Jens GutzeitJens Gutzeit Posts: 22Questions: 0Answers: 0
    Thanks, working fine now.
  • jhoguetjhoguet Posts: 3Questions: 0Answers: 0
    I wrote a quick plugin to override the string sorting to support booleans. Basically, I just force it to cast the boolean to a string. Hopefully this will help someone else.

    I am using v.1.5.6

    [code]
    (function($){

    var fnConvertToString = function(value){
    return (value + '').toLowerCase();
    };

    var fnEquals = function(lhs, rhs){
    if (lhs < rhs){
    return -1;
    }
    else if (lhs > rhs){
    return 1;
    }
    return 0;
    };

    var fnCompareString = function(lhs, rhs){
    var lhs = fnConvertToString(lhs);
    var rhs = fnConvertToString(rhs);
    return fnEquals(lhs, rhs);
    };

    $.fn.dataTableExt.oSort[ 'string-asc' ] = function(lhs, rhs){
    return fnCompareString(lhs, rhs);
    };

    $.fn.dataTableExt.oSort[ 'string-desc' ] = function(lhs, rhs){
    return fnCompareString(lhs, rhs) * -1;
    };
    })(jQuery);
    [/code]
This discussion has been closed.