Nested object data (arrays) with dynamic length

Nested object data (arrays) with dynamic length

ExerExer Posts: 3Questions: 2Answers: 0
edited February 2015 in Free community support

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

  • allanallan Posts: 63,686Questions: 1Answers: 10,500 Site admin
    Answer ✓

    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 in aaData is a row. There is no way around that.

    If you want to show the Days array in a concatenated form you could use data: "Days[, ].FormDay" or if you want some other formatting use data: "Days" and then use columns.render as a function so you can control the output.

    Allan

  • ExerExer Posts: 3Questions: 2Answers: 0

    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.

This discussion has been closed.