Nested object data (arrays) with dynamic length
Nested object data (arrays) with dynamic length
Hello guys,
This is my problem:
{
"aaData": [
{
"Days": [
{
"FromDay": "2015-02-07",
"ToDay": "2015-02-09"
},
{
"FromDay": "2015-02-10",
"ToDay": "2015-02-12"
}
]
},
{
"Days": [
{
"FromDay": "2015-02-05",
"ToDay": "2015-02-06"
}
]
}
]
}
This is my JSON it has nested Array of object called "Days" but this array is dynamic ( it can have different number of objects in it ).
So my question is how can i draw this in my DataTable, what i did for now is :
"columns": [
{ "data": "Days.0.FromDay" },
{ "data": "Days.0.ToDay" },
]
I saw it here http://datatables.net/examples/ajax/objects_subarrays.html, It works, but only for first object in array. I would appreciate if anyone could tell me how to loop through each and show all array objects in same column... Thanks in advance
This question has an accepted answers - jump to answer
Answers
It depends on what you want the outcome to be. If you want every
Days
object to be a separate row you would need to transfer the array as each entry inaaData
is a row. There is no way around that.If you want to show the
Days
array in a concatenated form you could usedata: "Days[, ].FormDay"
or if you want some other formatting usedata: "Days"
and then usecolumns.render
as a function so you can control the output.Allan
Hey allan, first of all thanks for a quick reply,
data: "Days[, ].FormDay"
was what i needed, but its always good to know there's way to control my output.