Table data from a JSON object already memory?

Table data from a JSON object already memory?

GaryFGaryF Posts: 16Questions: 0Answers: 0
edited March 2011 in General
Hi. Is it possible to use a JSON object already in memory as the data source for a table please? My page requests a big lump of data as JSON which contains many things to feed other "widgets" on the page and it also includes some data that needs to be shown in a table. I realise DataTables can request JSON data by itself through AJAX, but the data has already been loaded into my page for other uses and it would be very inefficient to allow DataTables to request the same data again.

The JSON object is quite big so below is an example of its structure. Assuming the var name is "data" I just want to give data.ForDataTables to DataTables so it can display that data.

[code]
{
"ForOtherWidgets": [
{
"date": "15 Mar 2011",
"name": "Rob Smith",
"speed": "96"
}
],
"ForDataTables": [
{
"description": "Blah blah",
"title": "Space craft",
"address": "Mars",
"type": "travel"
},
{
"description": "Yummy yummy",
"title": "Buildings",
"address": "Empire State",
"type": "city"
}
]
}
[/code]

Is this possible please? (And how!)

Thank you.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    It's not possible to give DataTables the "ForDataTables" array directly, because it doesn't know what to do with the properties of the objects in the array. What needs to happen is a conversion from your array of objects to an array or arrays - which you can then pass to DataTables for display. A simple for loop to construct the 2D array would do it.

    Regards,
    Allan
  • GaryFGaryF Posts: 16Questions: 0Answers: 0
    Thanks Allan. I just thought it was worth asking as you've added so many gems to DataTables over the years!

    Best regards,
    Gary.
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Support for associative arrays will be included in 1.8 :-). not 100% sure how it will be implemented just yet, but there will be support for basic object input :-)

    Regards,
    Allan
This discussion has been closed.