sorting questions

sorting questions

qoevanqoevan Posts: 1Questions: 0Answers: 0
edited May 2012 in General
Hi All,

DataTables noob question.

I'm using fnServerData to fetch JSON from a server and aoColumns to specify column info. Recently, there was a change to the JSON that my client consumed which broke the sort feature of the table. To resolve the issue I created a function for mDataProp which enabled sorting to work again. I'm just not sure if what I ended up doing is the best way of addressing the issue.

Before my JSON changed the column of interest used to come as a simple string like "foo" or "bar". Sorting on the column worked fine. After the server's update the JSON for that particular column now comes to the client as an array of strings like ["foo","bar"]. No problem I thought, because DataTables presented the data as a nice comma delimited string like "foo,bar" but, when I tried to sort on that column the column would not sort. What I did to resolve the issue was to make mDataProp a function that returns the toString() result of the JSON array when the 'type' argument is 'sort'.

[code]
var fixSortProblemForArrayOfApps = function(source,type,val) {
if ( type === 'sort' )
return source.apps.toString();
return source.apps;
};
[/code]

And my aoColumns property looks like:

[code]
"aoColumns": [
...
{"sTitle":"apps","mDataProp":fixSortProblemForArrayOfApps,"sDefaultContent":""},
...
],
[/code]

My questions are, "why isn't this the default behavior" and, "is my work around the right way to handle it"?

Thanks!
Evan
This discussion has been closed.