So if you are using the YUI DataTable with XHR and numbers you might have noticed the sort is not working correctly. Below is a quick explination and a work around.
“DataTable currently sorts strings, numbers, and dates, but only when the values are already those types in JavaScript. In contrast, if you get values over the wire (XHR), DataTable will treat all values as strings in those cases. As a workaround, you could implement a custom sort function to do the data type conversion. Data parsed from markup will be typed correctly, and so we’re a part of the way there; before we get out of beta we plan to provide a mechanism so a value can be treated as a specific type regardless of its origin. I can tell you that in the next release (coming soon) we’ll have a hook for you to do your own conversion of data coming over XHR, and in the subsequent release it will do a lot of conversions for you out of the box and also provide a mechanism for you to do more sophisticated custom conversions.”
Here is the custom sort function I created to fix the issue. It still doesn’t work well if you have multipage result lists because the string has to go through the function before it becomes an integer. It works well for small lists or where all the results appear right away.
In the snippet below, “u” would be the variable you want to get sorting correctly in the data grid results. If you are not familiar with using custom sort functions take a look at the yui data grid docs. It’s very easy.
[code lang="javascript"]
var myFormatStringFix =
function(elCell, oRecord, oColumn, oData)
{
oRecord._oData.u = parseInt(oRecord._oData.u);
elCell.innerHTML = oRecord._oData.u;
}
[/code]

Posted in
Tags: