Bug in the Alt string plugin

Bug in the Alt string plugin

KulgarKulgar Posts: 4Questions: 0Answers: 0
edited January 2013 in Plug-ins
Hi everyone,

There is a bug with the 'alt-string' sorting function in the "sorting" plugin:

[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"alt-string-pre": function ( a ) {
return a.match(/alt="(.*?)"/)[1].toLowerCase();
},

"alt-string-asc": function( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"alt-string-desc": function(a,b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
[/code]

I think it works well if all cells have an image, but if one doesn't (and it surely may happen), it breaks the code, saying:

[quote]a.match(...) is null[/quote]

It's because a is an empty string if the cell is empty. It may happen, for instance, when you have a column containing ticks but no crosses (better for readability). So as a quick workaround I did this:

[code]
"alt-string-pre": function ( a ) {
if (a == "") value = "zzz";
else value = a.match(/alt="(.*?)"/)[1];
return value.toLowerCase();
}
[/code]

Of course it's a quick fix and if one has a better solution, I'll be glad to hear it.

Bybye,
Kulgar.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Sounds like a perfectly reasonable solution for the case where there is no alt tag. That's the thing with each of these plug-ins, they might need altered slightly to cope with the different data that is used. This looks like a good change to put into the plugin though :-)

    Allan
  • KulgarKulgar Posts: 4Questions: 0Answers: 0
    I've made a little improvement, because there is time when you may also have text in the column, like "N.A." for "not applicable". And then it breaks again because "a" is no longer empty.

    So here is the improvement:

    [code]
    "alt-string-pre": function ( a ) {
    if (a.match(/alt="(.*?)"/) === null)
    if (a === "") value = "zzz";
    else value = a;
    // or "zza" if you don't want to take the text into account for sorting the column
    else value = a.match(/alt="(.*?)"/)[1];
    return value.toLowerCase();
    },
    [/code]
This discussion has been closed.