How to redraw DataTable with new data

JqueryDatatables

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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();

Solution 3 - Jquery

I was having same issue, and the solution was working but with some alerts and warnings so here is full solution, the key was to check for existing DataTable object or not, if yes just clear the table and add jsonData, if not just create new.

            var table;
            if ($.fn.dataTable.isDataTable('#example')) {
                table = $('#example').DataTable();
                table.clear();
                table.rows.add(jsonData).draw();
            }
            else {
                table = $('#example').DataTable({
                    "data": jsonData,
                    "deferRender": true,
                    "pageLength": 25,
                    "retrieve": true,

Versions

  • JQuery: 3.3.1
  • DataTable: 1.10.20

Solution 4 - Jquery

The following worked really well for me. I needed to redraw the datatable with a different subset of the data based on a parameter.

table.ajax.url('NewDataUrl?parameter=' + param).load();

If your data is static, then use this:

table.ajax.url('NewDataUrl').load();

Solution 5 - Jquery

datatable.rows().iterator('row', function ( context, index ) {
    var data = this.row(index).data();
    var row = $(this.row(index).node());
    data[0] = 'new data';
    datatable.row(row).data(data).draw();
});

Solution 6 - Jquery

If you want to refresh the table without adding new data then use this:

First, create the API variable of your table like this:

var myTableApi = $('#mytable').DataTable(); // D must be Capital in this.

And then use refresh code wherever you want:

myTableApi.search(jQuery('input[type="search"]').val()).draw() ;

It will search data table with current search value (even if it's blank) and refresh data,, this work even if Datatable has server-side processing enabled.

Solution 7 - Jquery

Add this snippet in your jquery code to destroy, clear using datatable and also it will work for the new columns if you want to add new column each time on changes event or any other event.

if ($.fn.DataTable.isDataTable("#dataListTable")) {
		$('#dataListTable').DataTable().clear().destroy();
	}	
	$('#dataListTable').empty();

Thanks, It works for me hope work for you also

Solution 8 - Jquery

Another alternative is

dtColumns[index].visible = false/true;

To show or hide any column.

Solution 9 - Jquery

  1. init: var grid = $('#dataTable').DataTable({});
  2. destroy: grid.destroy();
  3. replace the content of tbody tag, eg: $('#tableBodyId').empty().append(new_data);
  4. reinit: grid = $('#dataTable').DataTable({});

Solution 10 - Jquery

If columns doesn't change, parameters object can be configured as function. DataTable will be updated with each value change

"ajax": {
        "url": "/warehouse/getall",
        "type": "GET",
        "dataType": "json",
        data: function (d) {
            d.storeId = document.getElementById("StoreList").value;
            d.warehouseId = document.getElementById("WarehouseList").value;
        }

StoreList and WarehouseList are user combos.

Then with each combo change DataTable is updated with current values

$("#WarehouseList").change(function () {
    $("#tblData").DataTable().ajax.reload();
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2881063View Question on Stackoverflow
Solution 1 - JquerySSAView Answer on Stackoverflow
Solution 2 - JquerySkeetsView Answer on Stackoverflow
Solution 3 - JquerySandip BantawaView Answer on Stackoverflow
Solution 4 - Jqueryuser3101152View Answer on Stackoverflow
Solution 5 - JqueryvlasiliyView Answer on Stackoverflow
Solution 6 - JqueryDen PatView Answer on Stackoverflow
Solution 7 - JqueryNikhil HoleView Answer on Stackoverflow
Solution 8 - JquerysankyyyView Answer on Stackoverflow
Solution 9 - JqueryTomoMihaView Answer on Stackoverflow
Solution 10 - JqueryJaime VasquezView Answer on Stackoverflow