Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually update datatables table with new JSON data

I am using plugin jQuery datatables and load my data which I have loaded in DOM at the bottom of page and initiates plugin in this way:

var myData = [     {         "id": 1,         "first_name": "John",         "last_name": "Doe"     } ];  $('#table').dataTable({     data: myData         columns: [         { data: 'id' },         { data: 'first_name' },         { data: 'last_name' }     ] }); 

Now. after performing some action I want to get new data using ajax (but not ajax option build in datatables - don't get me wrong!) and update the table with these data. How can i do that using datatables API? The documentation is very confusing and I can not find a solution. Any help will be very much appreciated. Thanks.

like image 235
Indy Avatar asked Jan 05 '15 11:01

Indy


People also ask

What is Serverside in DataTables?

DataTables' server-side processing mode is a feature that naturally fits with Scroller. Server-side processing can be used to show large data sets, with the server being used to do the data processing, and Scroller optimising the display of the data in a scrolling viewport.

How much data can DataTables handle?

The maximum number of rows that a DataTable can store is 16,777,216.

What is stateSave in DataTables?

When DataTables' state saving option is enabled ( stateSave ) KeyTable will automatically store the last cell focused in a table and then restore that state when the page is reloaded.


1 Answers

SOLUTION: (Notice: this solution is for datatables version 1.10.4 (at the moment) not legacy version).

CLARIFICATION Per the API documentation (1.10.15), the API can be accessed three ways:

  1. The modern definition of DataTables (upper camel case):

    var datatable = $( selector ).DataTable();

  2. The legacy definition of DataTables (lower camel case):

    var datatable = $( selector ).dataTable().api();

  3. Using the new syntax.

    var datatable = new $.fn.dataTable.Api( selector );

Then load the data like so:

$.get('myUrl', function(newDataArray) {     datatable.clear();     datatable.rows.add(newDataArray);     datatable.draw(); }); 

Use draw(false) to stay on the same page after the data update.

API references:

https://datatables.net/reference/api/clear()

https://datatables.net/reference/api/rows.add()

https://datatables.net/reference/api/draw()

like image 175
Indy Avatar answered Sep 26 '22 02:09

Indy