Nested JSON data not appear in table

Nested JSON data not appear in table

mastersusemastersuse Posts: 61Questions: 28Answers: 0

How to print result from nested JSON like below? I have read multiple answer here but not solve.

This is what I have tried to do but No records to display appear.

DATATABLE

$('#myTable').DataTable({
    processing: true,
    language: {
        zeroRecords         : "No matching records found",
        loadingRecords      : "<img src='img/icon/ani_loading_1.gif'/>",
        emptyTable          : "No records to display",
        searchPlaceholder   : 'Search...',
        sSearch             : '',
        lengthMenu          : '_MENU_ items'
    },
    destroy: true,
    ajax: {
        url: url_pw_route,
        crossDomain : true,
        type : "POST",
        cache : false,
        dataType : "json",
        contentType: "application/json",
        dataSrc : "pw_route.0.pw_route_xc.0",
        data: function(d) {
            return JSON.stringify({"action": "read", "pw_id": pw_id });
        }
    },
    columns: [
        { data : "ne_name", "className": "text-center" }
    ],
});

JSON DATA

{
    "result": "success",
    "pw_route": [
        {
            "pw_route_id": "1",
            "pw_id": "11",
            "pw_route_xc": [
                {
                    "pw_route_id": "11",
                    "ne_name": "NE1"
                },
                {
                    "pw_route_id": "22",
                    "ne_name": "NE2"
                }
            ]
        },
        {
            "pw_route_id": "2",
            "pw_id": "22",
            "pw_route_xc": [
                {
                    "pw_route_id": "333",
                    "ne_name": "NE3"
                }
            ]
        }
    ]
}

This question has an accepted answers - jump to answer

Answers

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

    I think you will want something more like this:

        ajax: {
            url: url_pw_route,
            crossDomain : true,
            type : "POST",
            cache : false,
            dataType : "json",
            contentType: "application/json",
            dataSrc : "pw_route",
            data: function(d) {
                return JSON.stringify({"action": "read", "pw_id": pw_id });
            }
        },
        columns: [
            { data : "pw_route_xc.0.ne_name", "className": "text-center" }
        ],
    

    Like this:
    http://live.datatables.net/hurebeta/1/edit

    You can use columns.render if you want to display all the ne_name in pw_route_xc.

    Kevin

  • mastersusemastersuse Posts: 61Questions: 28Answers: 0

    Thank you, this solve my problem.

This discussion has been closed.