Sorting HTML Data Doesn't Convert to Float
Sorting HTML Data Doesn't Convert to Float
ofosho
Posts: 5Questions: 0Answers: 0
My apologies if this is not a bug, but rather a choice.
I found that when a table contains html data in the td, specifically a div in my case, datatables properly recognizes and removes the tags. However, it then uses a generic (x < y) to sort the string. This caused odd sorting errors in some of my colums. Specifically, numbers in the 1000 and above range where considered smaller than numbers with 3 or less digits. I am not sure why, but the following addition to the sorting function fixed it:
"html-asc": function ( a, b )
{
var x = a.replace( /<.*?>/g, "" ).toLowerCase();
var y = b.replace( /<.*?>/g, "" ).toLowerCase();
if(!isNaN(x))
x = parseFloat(x);
if(!isNaN(y))
y = parseFloat(y);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
},
"html-desc": function ( a, b )
{
var x = a.replace( /<.*?>/g, "" ).toLowerCase();
var y = b.replace( /<.*?>/g, "" ).toLowerCase();
if(!isNaN(x))
x = parseFloat(x);
if(!isNaN(y))
y = parseFloat(y);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
},
I am sure there is a more efficient, or at least cleaner, way of doing that (perhaps adding an isnum function), but I didn't really want to mess with the code too much.
Again, if this was by choice and I am supposed to make my own sort function for HTML data I apologize.
Thanks,
-O
I found that when a table contains html data in the td, specifically a div in my case, datatables properly recognizes and removes the tags. However, it then uses a generic (x < y) to sort the string. This caused odd sorting errors in some of my colums. Specifically, numbers in the 1000 and above range where considered smaller than numbers with 3 or less digits. I am not sure why, but the following addition to the sorting function fixed it:
"html-asc": function ( a, b )
{
var x = a.replace( /<.*?>/g, "" ).toLowerCase();
var y = b.replace( /<.*?>/g, "" ).toLowerCase();
if(!isNaN(x))
x = parseFloat(x);
if(!isNaN(y))
y = parseFloat(y);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
},
"html-desc": function ( a, b )
{
var x = a.replace( /<.*?>/g, "" ).toLowerCase();
var y = b.replace( /<.*?>/g, "" ).toLowerCase();
if(!isNaN(x))
x = parseFloat(x);
if(!isNaN(y))
y = parseFloat(y);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
},
I am sure there is a more efficient, or at least cleaner, way of doing that (perhaps adding an isnum function), but I didn't really want to mess with the code too much.
Again, if this was by choice and I am supposed to make my own sort function for HTML data I apologize.
Thanks,
-O
This discussion has been closed.
Replies
Allan
Thanks,
-O