Possible to flatten children into rows?

Possible to flatten children into rows?

larrybudlarrybud Posts: 3Questions: 3Answers: 0

Been looking but not finding an answer... is it possible for datatables to flatten nested data? Given the following json:

{
    "employee": [
        {
            "id": 1,
            "name": "Dave",
            "addresses": [
                {
                    "address": "123 fake street",
                    "zip": "12345"
                },
                {
                    "address": "555 Main Street",
                    "zip": "90210"
                },
            ]
        },
        {
            "id": 2,
            "name": "Steve",
            "addresses": [
                {
                    "address": "923 Lyon Oaks",
                    "zip": "55555"
                }
            ]
        }
    ]
}

I would like the end result to display 3 rows, one for each address. In this example, "dave" would be listed twice (his name repeated, once for each address he has).

Or do I have to flatten it myself on the backend?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    DataTables wouldn't support that, so yep, you need to flatten it somewhere - either on the back-end as you say or on the browser using ajax.dataSrc,

    Colin

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    Answer ✓

    One option is to use Child Detail Rows and display the addresses in the child rows. You could have them open by default.

    Datatables doesn't have a built in way to split that data structure, Dave for example, into two rows. If you don't want to use Child Details then you will need to flatten the data before giving it to Datatables.

    Kevin

This discussion has been closed.