1

HTML

<table id='students_grid' class='table table-bordered table-striped' cellpadding='0' cellspacing='0' >
       <thead>
        <tr>
        <th>Student ID</th> /*Hidden*/
        <th>Sr. No.</th>
        <th>Student Name</th>
        <th>Address</th>
        <th>Parent Code</th> /*Hidden*/
        <th>Parent Name</th>                                    
      </tr>
    </thead>
    <tbody>
    </tbody>
</table>

JS Initialisation

$("#students_grid").DataTable({       
"select": {style: 'single'},      
"columnDefs": [
    {
        "data":"studentid",
        "targets": [ 0 ],
        "visible": false,
        "searchable": false
    },
    {
        "data":"parentcode",
        "targets": [ 4 ],
        "visible": false,
        "searchable": false
    }
]
});

Adding data in Table (Called on Button Click)

studenttable = $("#students_grid").DataTable();
studenttable.row.add( [
    $("#studentid").val(),
    $("#srno").val(),
    $("#studentname").val(),            
    $("#address").val(),
    $("#parentcode").val(),
    $("#parentname").text()
]).draw(false);

After the add function is called an warning message is popped up

DataTables warning: table id=students_grid - Requested unknown parameter 'studentid' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

Although the row gets added in the table this warning keeps popping up with each addition the row value changes with each addition. How can this warning be avoided?

UPDATE If I remove the "data" declaration from the initialisation it does not give the error message. Can this not be done with the "data" value

7
  • Are you passing a header row in the json? That would account for the row 0, column 0 value being incorrect. The answer, I think, depends on the data that is being passed to DataTables Commented Jul 19, 2022 at 8:31
  • @KScandrett I am only adding one row at a time header row is already added through HTML page. Even if I try to add static values the warning message is show. In spite of the warning the newly added data does show up in the table. Commented Jul 19, 2022 at 8:36
  • 2
    You have no named parameters in your data, just values. So it's never going to find something called 'studentid'. Commented Jul 19, 2022 at 8:38
  • 1
    AddRow example from their website: table.row.add( { "name": "Tiger Nixon", "position": "System Architect" } ) Commented Jul 19, 2022 at 8:40
  • 2
    Did you read the details on the link it provides? It's all explained there: "Parameter is a string. If {parameter} is shown as a string, this will typically indicate that you have used columns.data or columns.render to pull data out of an object" - but you're passing an array, you need to pass an object: .row.add({ studentid: $("#studentid").val()... Commented Jul 19, 2022 at 8:57

1 Answer 1

2

The data is missing parameter names so you will see that error if you try to reference it by one.

In addition, you're passing it an array when it is expecting an object for a single row or an array of objects for multiple rows.

studenttable.row.add({
  "studentid": $("#studentid").val(),
  "srno": $("#srno").val(),
  "studentname": $("#studentname").val(),
  "address": $("#address").val(),
  "parentcode": $("#parentcode").val(),
  "parentname": $("#parentname").text()
}).draw(false);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.