Adding to an empty table
Adding to an empty table

I am adding elements to data table using Jquery.
$('#finalPlayListTable tr:last').after('<tr><td>' + reportTitle + '</td><td>' + analystName + '</td><td>' + dateTime + '</td><td>' + status + '</td></tr>');
I need to get the all the row data of the finalPlayListTable. If the data table is already initialized I am able to use
$('#finalPlayListTable').DataTable().rows().data().toArray();
if Final table has no elements and I am using the above code to push the elements I am unable to get the row data using
$('#finalPlayListTable').DataTable().rows().data().toArray();
This question has accepted answers - jump to:
This discussion has been closed.
Answers
The method you are using adds to the table but Datatables does not know about the added row. A better option is to use Datatables API to add the row, like
row.add()
. Otherwise you will need to use an API for Datatables to update its data cache. Something likerows().invalidate()
.Kevin
I did not understand how using rows().invalidate() can help me.
Can you elaborate your answer?
Vineela.
I built an example but was not able to get your method to add a row to work if Datatables is initialized. The example also shows how to use
row.add()
which is what I suggest you use.http://live.datatables.net/pukapomo/1/edit
Kevin
Thanks Kevin, this helped!
This answered the question, but there is an another problem.
I add data to dynamic data table and get the row information in a json like I passed working now but when I use
to get the data back in a json form that I send it.
But when I initialize the table like you did in your example and use
I got
How do I get the same json for the second case too?
Hi @vineela374 ,
Could you update Kevin's test case to demonstrate the problem, please.
Cheers,
Colin
If your data is object based then you will use
columns.data
to define the columns. Since your row data is an array of objects you can userows.add()
to add all the rows at once. Here is an example:http://live.datatables.net/temunihu/1/edit
Kevin