How can i output a multilevel json object ?

How can i output a multilevel json object ?

mdesignmdesign Posts: 72Questions: 17Answers: 0
edited April 16 in DataTables

How can i output a multilevel json object. can anyone help me with the correct syntax für the columns. see this simple case for example.

/* table rows should look like this */
Summer 2024 | Team Blue 1 | Nr.1 | Hans
Summer 2024 | Team Blue 1 | Nr.2 | Noah
Summer 2024 | Team Pink 2 | Nr.1 | Emma
Summer 2024 | Team Pink 2 | Nr.2 | Susi

<table id="example">
  <thead>
    <tr>
      <th>seasonName</th>
      <th>contestType</th>
      <th>teamNr</th>
      <th>fedRank</th>
      <th>firstname</th>
    </tr>
  </thead>      
  <tbody></tbody>
</table>
<script>
var thisDataTable = $('#example').DataTable({    
  ajax: 'file.json',
  columns: [ ??? ],
}); // thisDataTable
</script>


/* file.json */
{
  "data": [
    {
      "seasonName": "Summer 2024",
      "teams": [
        {
          "contestType": "Team Blue",
          "players": [
              {
                "rank": 1,
                "firstname": "Hans",
              },
              {
                "rank": 2,
                "firstname": "Noah",
              },
          ]
        },
        {
          "contestType": "Team Pink",
          "players": [
              {
                "rank": 1,
                "firstname": "Emma",
              },
              {
                "rank": 2,
                "firstname": "Susi",
              },
          ]
        },
      ]
    }
  ]
}

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    If you want that output, you'd need to preprocess the JSON data to fold it down. DataTables will display one row per entry in the data array that it is given to show. It cannot show nested data like that over multiple rows.

    Allan

  • mdesignmdesign Posts: 72Questions: 17Answers: 0

    thx allan, that saves me time

Sign In or Register to comment.