Passing JSON object as data source

Passing JSON object as data source

cschnaarscschnaars Posts: 8Questions: 0Answers: 0
edited June 2011 in General
Hi, everyone:

I've been poking around online for an answer to this question but haven't been able to find one.

A lot of the data tables I build use a JSON object stored in a .js file as a data source. Like this:

[code][
["AK","Anchorage"," 291,826 ","40.1%","39.9%"," 0.2 "],
["AL","Auburn"," 53,380 ","55.6%","59.1%"," -3.5 "],
... more ...
][/code]

However, to get them to work, I've had to change the files to variable declarations and bring them in as scripts like this:
[code]tabledata = [
["AK","Anchorage"," 291,826 ","40.1%","39.9%"," 0.2 "],
["AL","Auburn"," 53,380 ","55.6%","59.1%"," -3.5 "],
... more ...
];[/code]

Then I just set aaData to this variable name to marry the JSON data to the data table.

I am guessing this is not the best or most efficient way to handle this, particularly since it means modifying a valid JSON file. But I haven't been able to figure out another way to bring in the data. If someone could point me in the right direction, I'd really appreciate it. For what it's worth the JSON file is in the same directory as the .html file.

Thank you,
chris

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    This is the kind of thing that DataTables expects if you are using Ajax sourced data: http://datatables.net/release-datatables/examples/ajax/sources/arrays.txt . If you want to just feed DataTables an array - then assigning it to a variable and giving it as aaData if just fine.

    Allan
  • cschnaarscschnaars Posts: 8Questions: 0Answers: 0
    Thanks very much for the quick response, Allan.

    chris
This discussion has been closed.