Render child Json data in datatable columns

Render child Json data in datatable columns

Swf1200Swf1200 Posts: 3Questions: 2Answers: 0

I have this JSON response for datatable:

{
    "draw": "1",
    "iTotalRecords": 201,
    "iTotalDisplayRecords": 35,
    "aaData": [
        {
            "id": "265",
            "date": "2022-02-14 01:11:29",
            "ip_address": "127.0.0.1",
            "user_agent": "{\"information\":{\"browser\":\"Firefox\",\"platform\":\"Windows 10\",\"mobile\":\"\"}}",
            "success": "1"
        }
    ]
}

Also, I have child JSON in user_agent and need to show this information in datatable columns.

Datatable JS:

var tableUserActivity = $('#tableUserActivity').DataTable({
    dom: 'rt<"d-md-flex justify-content-between align-items-center" <i> <p>>',
    processing: true,
    serverSide: true,
    responsive: true,
    order: [
        [1, 'desc']
    ],
    ajax: {
        url: '<?= route_to('getUserActivity') ?>',
        method: 'POST',
        'data': function(data) {
            // Read values
            var dateFrom = $('#searchDateFromAlt').val();
            var dateTo = $('#searchDateToAlt').val();
            data.id = '<?= esc($user['id']) ?>';
            // Append to data
            data.filterDateFrom = dateFrom;
            data.filterDateTo = dateTo;
        }
    },
    columnDefs: [{
        orderable: false,
        targets: [0, 2, 3]
    }],
    columns: [{
            'data': 'id',
            'class': 'text-center align-middle'
        },
        {
            'data': 'date',
            'class': 'align-middle'
        },
        {
            data: 'user_agent',
            render: function(data, type, full_row) {
                
                return console.log(full_row.information.browser);
            }
        },
        {
            'data': 'ip_address',
            'class': 'text-center align-middle'
        }
    ]
});

Now in action I see this error:

full_row.information is undefined

How do can I fix this error and show JSON data in columns?!

Answers

Sign In or Register to comment.