Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redraw DataTable with new data

I have checked several questions already about this topic here in stackoverflow, but they are all using the old dataTable. I am using DataTable. I populated my DataTable by NOT USING server side, so data are preloaded (JSON) like this :

datatable = $("#datatable").DataTable({    data  : myData,    moreoptions : moreoptions }); 

I didn't have a problem with that, the DataTable loaded just fine. Now I want to re-populate that myData with new data i uploaded. How to reload the DataTable to reflect the changes?

Here's what I have tried so far :

$('#upload-new-data').on('click', function () {    myData = NewlyCreatedData; // I console logged this NewlyCreatedData, and it has my uploaded data.     datatable.draw(); // Redraw the DataTable }); 

But this doesn't work. I also tried this :

datatable = $("#datatable").DataTable({    "data"  : myData,    "drawCallback" : function () {       myData = NewlyCreatedData;    },    "moreoptions" : moreoptions, }); 

Then on upload I just call the redraw trigger :

$('#upload-new-data').on('click', function () {    datatable.draw(); // Redraw the DataTable }); 

Still this doesn't work.

like image 916
user2881063 Avatar asked Sep 19 '14 08:09

user2881063


People also ask

Is dataTable editable?

Create customised, fully editable tables. in minutes with Editor for DataTables. The entire row in a DataTable can be easily edited in Editor using the main editing interface.

How do you refresh a dataTable without losing your current page or ordering?

Thankfully one of the DataTables forum guys came up with a handy bit of extension code which introduces a new function called fnStandingRedraw() which extends the datatables API and allows you to fresh the datatable without losing your current view. //var oTable1 = $('#mytable'). dataTable(); oTable1.

What is FN dataTable ext search push?

fn. dataTable. ext.search . This is an array of functions (push your own onto it) which will will be run at table draw time to see if a particular row should be included or not. This example shows a search being performed on the age column in the data, based upon two inputs.


2 Answers

You have to first clear the table and then add new data using row.add() function. At last step adjust also column size so that table renders correctly.

$('#upload-new-data').on('click', function () {    datatable.clear().draw();    datatable.rows.add(NewlyCreatedData); // Add new data    datatable.columns.adjust().draw(); // Redraw the DataTable }); 

Also if you want to find a mapping between old and new datatable API functions bookmark this

like image 128
SSA Avatar answered Sep 21 '22 16:09

SSA


The accepted answer calls the draw function twice. I can't see why that would be needed. In fact, if your new data has the same columns as the old data, you can accomplish this in one line:

datatable.clear().rows.add(newData).draw(); 
like image 36
Skeets Avatar answered Sep 23 '22 16:09

Skeets