Add new row to DataTable dynamically
Add new row to DataTable dynamically
Hi everyone,
I got stuck in adding a row to a datatable. on a page load i am loading data into datatable. I am trying to add a row to the same datatable by creating a instance of it. It says unknow parameter, I couldn't find any issues with data or the parameter. Please help.
Here is my
JSON when the page laods
{
"ID": 1,
"UserName": "NA",
"CallStatus": "",
"Cancelled": null,
"Completed":null,
"CompletedPercentage": null,
"ConvDate":null,
"CreatedorModifiedDate":"/Date(-2208970800000)/",
"EmailID": null,
"Favorites": 0,
"Firstname": "ANNIE",
"Image": "default_user.png",
"PhoneNumber": "",
}
I am trying to add row with JSON which has same items, but it says unknown parameter for phonenumber. Phone number is a string data type in my code.
Here is the code how i am adding a row to datatable
/Creating a instance of the datatable/
var newTable = $('#table2').DataTable();
/Adding row/
newTable.row.add([{
"ID": 1,
"UserName": "NA",
"CallStatus": "",
"Cancelled": null,
"Completed":null,
"CompletedPercentage": null,
"ConvDate":null,
"CreatedorModifiedDate":"/Date(-2208970800000)/",
"EmailID": null,
"Favorites": 0,
"Firstname": "ANNIE",
"Image": "default_user.png",
"PhoneNumber": "",
}]).draw();
this is the version i am using https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js
Thank you
Answers
The
row.add()
api expects only one row and that the object data is not in an array. The example in the doc shows this. You are adding the object within an array.However,
rows.add()
does expects an array of the object(s) to add.You can either use
rows.add()
with your current data within an array or just add the object withrow.add()
.Kevin
where is your code that defines the column names?
I should see something like
var newTable = $('#table2').DataTable({
columns:[
{data:"ID"}, {data:"UserName"}
]});
for each column you want to show in the table. Also, these names are case sensitive.
take a look at https://datatables.net/reference/option/columns
Hi kthorngren. row.add worked but i am not sure why its not adding to the table. i am not getting any errors.When i try to run same code in Console tab of chrome, it adds a row, but when i do the same thing in javascript file of the page it does nothing. Bindrid i really appreciate your quick response.
We'd really need to be able to see your page to be able to help in that case. Can you give us a link please?
Allan