sAjaxSource: table initializations fails when using an array of objects

sAjaxSource: table initializations fails when using an array of objects

nunonuno Posts: 14Questions: 0Answers: 0
edited June 2012 in Bug reports
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!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Have a look at this blog post about using an array of objects: http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Allan
  • nunonuno Posts: 14Questions: 0Answers: 0
    Thanks for your reply 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
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Can you link me to a page showing the problem please?

    Allan
  • nunonuno Posts: 14Questions: 0Answers: 0
    The page is not online but I can reproduce the error. I've changed key "0" to 0.

    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]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Can you run your table through the DataTables debugger and let me know the code please?

    Allan
  • nunonuno Posts: 14Questions: 0Answers: 0
    I tried that before and just tried again but it makes my Firefox crash. On Chrome I left it for over 10 min and it never completed...

    Sorry!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I'm really sorry then - but there is little I can do to offer any help. All I can say is that error will be given only when the JSON return is invalid.

    Allan
This discussion has been closed.