Populate Table with Nested Ajax Source Where Key Names Not Known

Populate Table with Nested Ajax Source Where Key Names Not Known

anthony89anthony89 Posts: 26Questions: 8Answers: 0

Is it possible to populate a table using an ajax source where the nested keys are not known? For example, take this ajax data source:

{
    "success": true,
    "statistics": {
        "bucket": "logs",
        "prefix": "media2021-03-10",
        "data": {
            "2003%2Fmp3_lq%2F2003_1207_ses.mp3": {
                "filename": "2003_1207_ses.mp3",
                "downloads": 1,
                "bytes_sent": 1392640,
                "size": 9951232
            },
            "2003%2F2003_1207_ses.mp4": {
                "filename": "2003_1207_ses.mp4",
                "downloads": 1,
                "bytes_sent": 0,
                "size": 0
            }
        }
    }
}

I want the table columns to be defined with the filename, downloads, bytes_sent, and size fields from the ajax data source.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,177Questions: 26Answers: 4,923
    edited March 2021 Answer ✓

    Datatables does support this structure. Datatables supports an array of objects, more like this:

    {
        "success": true,
        "statistics": {
            "bucket": "logs",
            "prefix": "media2021-03-10",
            "data": [
                {
                    "filename": "2003_1207_ses.mp3",
                    "downloads": 1,
                    "bytes_sent": 1392640,
                    "size": 9951232
                },
                {
                    "filename": "2003_1207_ses.mp4",
                    "downloads": 1,
                    "bytes_sent": 0,
                    "size": 0
                }
            ]
        }
    }
    

    If your source isn't able to return this structure you will need to restructure it before populating Datatables. Your server script can do this or you can restructure in the client using ajax dataSrc (assuming you are using the ajax option) as a function.

    Kevin

  • anthony89anthony89 Posts: 26Questions: 8Answers: 0

    Thanks. I was able to restructure the array being returned to match along the lines you suggested and it is working now.

This discussion has been closed.