sAjaxSource: table initializations fails when using an array of objects
sAjaxSource: table initializations fails when using an array of objects
nuno
Posts: 14Questions: 0Answers: 0
Hi!
I was building a table and loading the data via an ajax request.
Initially I was returning an array as the data source and it worked fine.
Then I realized I wanted to set a class for each row and tought I'd change the data source to be an array of objects like:
[code]
{'aaData' => {
'DT_RowClass'=>dummy_class,
0=>s.acronym,
1=>s.name,
2=>s.com}
}
[/code]
I'd assume this would work as I've used such a data source in the past. The only difference was that before I was loading it with the page instead of fetching it via AJAX. However, I got an error saying that DataTables could not parse the JSON and no table. Firebug however could parse the JSON.
I've now finally got it to work with the following hash:
[code]
{'aaData' => {
'DT_RowClass'=>dummy_class,
'0'=>s.acronym,
'1'=>s.name,
'2'=>s.com}
}
[/code]
The difference is that all hash keys are strings!
I was building a table and loading the data via an ajax request.
Initially I was returning an array as the data source and it worked fine.
Then I realized I wanted to set a class for each row and tought I'd change the data source to be an array of objects like:
[code]
{'aaData' => {
'DT_RowClass'=>dummy_class,
0=>s.acronym,
1=>s.name,
2=>s.com}
}
[/code]
I'd assume this would work as I've used such a data source in the past. The only difference was that before I was loading it with the page instead of fetching it via AJAX. However, I got an error saying that DataTables could not parse the JSON and no table. Firebug however could parse the JSON.
I've now finally got it to work with the following hash:
[code]
{'aaData' => {
'DT_RowClass'=>dummy_class,
'0'=>s.acronym,
'1'=>s.name,
'2'=>s.com}
}
[/code]
The difference is that all hash keys are strings!
This discussion has been closed.
Replies
Allan
In my code I'm not setting "mDataProp" so it should just default to the integer values.
The problem I observed is that if I fetch the data via an AJAX request then the hash keys MUST be strings or else it won't work.
This behavior is different from when I preload the data in the page. In that case it accepts numbers as keys so there's an inconsistency!
It's probably a minor bug but took me some time to get it working because the error msg I got was saying I was getting invalid JSON while at the same time Firebug was parsing it.
Cheers,
Nuno
Allan
I've collected the error message;
[quote]
DataTables warning (table id = 'fleet_datatable'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.
[/quote]
Here's an extract of the JSON:
[code]
{"aaData": [
{"6": "2010.237.14.40.21",
"7": "",
0: "E2K",
"1": "H02",
"DT_RowClass": "unknown",
"2": "W48",
"3": ["Payload", 5],
"4": "19/20",
"5": ["2010.244.17.55.41", 85389],
"DT_RowId": "mimic_99"
},
[/code]
Allan
Sorry!
Allan