Adding rows to datatable from json object data

Adding rows to datatable from json object data

spillino72spillino72 Posts: 9Questions: 2Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

This question has an accepted answers - jump to answer

Answers

  • spillino72spillino72 Posts: 9Questions: 2Answers: 0

    Error messages shown: Active:982 Uncaught ReferenceError: device is not defined
    Description of problem: I'm using row.add to adding row in my datatable but it gives me an error.

    My code:

        $.ajax({
             type: "GET",
            dataType: 'json',
            url: '/AlarmMonitoring/CoreNetwork/Active/newAlarm',
                success: function(response) {
                    //console.log(response);
                    console.log(response['device']);
                    //var jsonObject = JSON.stringify(response);
                    //console.log(jsonObject);
    
               var table = $('#alarm_datatable').DataTable();
                    table.row.add([{
                        'Device': response['device'],
                        'Interface': response['component'],
                        'Interface decription': response['interface_description'],
                        'Message': response['message'],
                        'Time': response['Res_time'],
                        'Location': response['Location'],
                        'Sev': response['severity'],
                        'Count': response['count'],
                        'Status': response['eventState'],
                    }]).draw();
            },
        });
    
  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    Not sure specifically where that error is coming from but row.add() expects only one row so it shouldn't be placed in an array. You can either remove it from the array or use rows.add() (with an s) to add multiple rows from an array.

    If this doesn't help then please post a link to your page or a test case so we can look at what your script is doing.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • spillino72spillino72 Posts: 9Questions: 2Answers: 0

    Ok thanks. I'm now using rows.add(). It seems like working. however it returns: Requested unknown parameter "Res_time". It exists in the json object and have this value: "23-11-19 16:30:35". the same format initialized in the datatable

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

    Minimally post your Datatables init code, the JSON response from the browser's network inspector (steps in this technote). Better would be a link to your page or a test case replicating the issue so we can take a look? Without seeing it we will not be able to help debug. You can build a test case with an example of your JSON response as a Javascript variable.

    Kevin

  • spillino72spillino72 Posts: 9Questions: 2Answers: 0

    Sorry, you right.
    This is the test case
    http://live.datatables.net/yowakike/1/edit

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

    When using objects you need to define columns.data. Please see the Data Docs for more details.

    Kevin

  • spillino72spillino72 Posts: 9Questions: 2Answers: 0

    Columns data are defined in my original datatable. I tried this sample code:

     table.rows.add(response).draw();     
    

    In response I passed only the data defined in columns.data.
    no error, no raw added!!!

    Unsing jquery append method the rows was added. But I don't want use jquery because datatable doesn't reorder the rows added in this way.

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

    Columns data are defined in my original datatable

    Please update the test case with this and hopefully you can recreate the problem you are having.

    Unsing jquery append method the rows was added. But I don't want use jquery because datatable doesn't reorder the rows added in this way.

    This is true. Although you can use row().invalidate() if you go this route.

    Kevin

  • spillino72spillino72 Posts: 9Questions: 2Answers: 0

    OK thanks. I solved it using the rows.add () API. I wasn't using the for loop correctly in the Laravel method and client-side javascript. Thanks again for the support.

This discussion has been closed.