How to dynamically add a string without the "Requested Unknown Parameter" error
I am new to DataTables and I am having problems dynamically adding new row to datatable.
Here is my initialization:
table = $("#college-list").DataTable({
'ajax': {
'url': 'admin/get_college',
'type': 'GET'
},
'columns': [
{ 'data': 'college_abbrev', "bSortable": true },
{ 'data': 'college_name' , "bSortable": true },
{
"mData": null,
"bSortable": false,
"mRender": function(data, type, college) {
return '<button type="button" class="btn btn-primary edit-college table-condensed">Edit</button>'
+'<button data-id="' + college.college_id + '" type="button" class="delete-college btn btn-primary table-condensed" href="">Delete</button>';
}
}
]
});
Here is some sample code I am using when adding newline ( ca
, cn
and college_id
are variables):
table.row.add( [
{
"college_abbrev": ca,
"college_name": cn,
"button":'<button type="button" class="btn btn-primary edit-college table-condensed">Edit</button>'
+'<button data-id="' + college_id + '" type="button" class="delete-college btn btn-primary table-condensed" href="">Delete</button>'
}
]).draw();
It creates a row, but the columns are empty except for the buttons and it gives the following error:
DataTables Warning: table id = college-list - Unknown parameter 'college_abbrev' requested for line 17
How do I add a new line correctly?
source to share
Use the following code instead:
table.row.add({
"college_abbrev": ca,
"college_name": cn,
"college_id": college_id
}).draw();
You are getting a "Requested unknown parameter" error because you are passing an array of objects to row.add()
, where you must pass a simple object instead, see row.add () for more information.
Also you don't need to create a button when called row.add()
. Your function mRender
will take care of this for you. Instead, you need to go through college_id
because mRender
it will need it to create the button.
Typically, you need to pass string data row.add()
in the same format your Ajax script uses admin/get_college
.
source to share