Adding nested JSON to row.add()

Adding nested JSON to row.add()

kpaswinkpaswin Posts: 5Questions: 2Answers: 0
edited January 2016 in Free community support

Here is my code :

var dataTbl;
var columns = [{
        data: null
    }, {
        data: 'payee.name'
    }, {
        data: 'isExpense'
    }, {
        data: 'trans_type'
    }, {
        data: 'amount'
    }, {
        data: 'paid_amount'
    }, {
        data: 'reminder.due_date',
        defaultContent: ""
    }, {
        data: 'reminder.details',
        defaultContent: ""
   }]

dataTbl = $('#table2').DataTable({
            "ajax": {
                "url": url,
                "type": "GET",
                dataSrc: ''
            },
            columns: columns,
            "sPaginationType": "full_numbers",
            "columnDefs": [ {
            "searchable": false,
            "orderable": false,
            "targets": 0
        } ],
        "order": [[ 1, 'asc' ]]
        });

dataTbl.row.add({
                            "no": null,
                            "payee.name": rowData.payee.name,
                            "isExpense": rowData.isExpense,
                            "trans_type": rowData.trans_type,
                            "amount": rowData.amount,
                            "paid_amount": rowData.paid_amount,
                            "reminder.due_date": rowData.reminder.due_date,
                            "reminder.details": rowData.reminder.details,
                        }).draw();

The problem comes when trying to add a new row. Actually rowData is a three level nested JSON that is returned from the ajax post. The problem comes only in the fields of rowData.payee.name , rowData.reminder.due_date , rowData.reminder.details . All other fields are working fine while adding. How should I represent three level nested JSON on row.add() ?

Answers

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    Have you looked at this topic? Seems to cover what you are seeking, you need to define the columns and data in json nested format.

    https://datatables.net/examples/ajax/objects_subarrays.html

  • kpaswinkpaswin Posts: 5Questions: 2Answers: 0

    As I have said the problem only comes when trying to add a row in datatable. It works fine when initializing the datatable. Thanks anyway @glenderson

    Found the solution. It works fine when I use dataTbl.row.add(rowData).draw(); . It fills the columns directly from the columns parameter even while adding the row.

This discussion has been closed.