Use datatable to display unstructured object arrays.
Use datatable to display unstructured object arrays.
My ajax response will return an array of objects with variable key combinations, for example:
[
{key1: value, key2: value},
{key1: value, key3: value},
{key2: value, key3: value, key4: value}
]
I need to present these data in a tabular format, like:
key1 key2 key3 key4
value value blank blank
value blank value blank
blank blank value value
that's to say, I need to build the table according to the returned json array.
Is there any way to achieve this using data tables?
[
{key1: value, key2: value},
{key1: value, key3: value},
{key2: value, key3: value, key4: value}
]
I need to present these data in a tabular format, like:
key1 key2 key3 key4
value value blank blank
value blank value blank
blank blank value value
that's to say, I need to build the table according to the returned json array.
Is there any way to achieve this using data tables?
This discussion has been closed.
Replies
you might have to reorganize your json or array of objects to include all the keys. example:
[code]
{key1: value, key2: value,key3: value, key4: value},
{key1: value, key2: value,key3: value, key4: value},
{key1: value, key2: value,key3: value, key4: value},
{key1: value, key2: value,key3: value, key4: value},
[/code]
UPDATE: sorry for this miss info.
I think I need a function to build aoColumns property based on the ajax response, I don't know how.
I would be really appreciated if some one can give me some advices.
this is not correct but just an idea
[code]
$.getJSON('URL_TO_GET_DATA', function(data){
console.log(data); //check your data
var array = [];
for(key in data)
{
//set up your conditions to build proper array
if(data[key] == "key1")
{
array.push(data[key]);
}
if(data[key] == "key2")
{
array.push(data[key]);
}
//etc for all your conditions
}
});
[/code]
http://datatables.net/release-datatables/examples/data_sources/server_side.html has the example of how to structure your data
UPDATE: Sorry i am assuming you can change how the json is delivered. If not the yes i believe you have to use fnServerData
Allan
Is there a way that I can tell Datatable to pick up the object key as column, instead of defining each column by hand? because in this case, I don't know what's the columns are going to be exactly. I need to build them using returned data.
Allan
I would say if you are going to do that, then keep using objects - much more flexible.
Allan