Datatables.net – Update Ajax Interval, Reload Data, Refresh Table

 

“Also, many of the legacy plug-ins have been integrated directly into DataTables 1.10’s core API (for example fnReloadAjax‘s functionality is provided by ajax.reload()DT now.”

source: http://www.datatables.net/plug-ins/api/

 

 Working example: 

var table = $(‘#tabl1′).DataTable( {
“ajax”: ‘api/q/’ + json,
“pageLength”: 15,
“columns”: [
{ “data”: “a” },
{ “data”: “b” },
{ “data”: “c” },
{ “data”: “d” }
]
});

setInterval( function () {
table.ajax.reload();
}, 3000 );

 

 

 

 

 

Notes:

 

Check your declerations

Actually, the confusion here is over the difference between $().dataTable() which you are using and returns a jQuery object and the old DataTables API, and $().DataTable() which returns the new API instance.

This is noted in the manual and FAQs – but I’m very interested to hear any suggestions you might have about how I can improve the documentation?

Allan

 

http://datatables.net/reference/api/ajax.reload

ajax.reload()

Since: 1.10

Reload the table data from the Ajax data source

Description

In an environment where the data shown in the table can be updated at the server-side, it is often useful to be able to reload the table, showing the latest data. This method provides exactly that ability, making an Ajax request to the already defined URL (use ajax.url( )DT if you need to alter the URL).

Type

function ajax.reload( callback, resetPaging )

Parameters:
  Name Type Optional Description
1 callback

functionJS

Yes – default:null

Function which is executed when the data as been reloaded and the table fully redrawn. The function is given a single parameter – the JSON data returned by the server, and expects no return.

2 resetPaging

functionJS

Yes – default:true

Reset (default action or true) or hold the current paging position (false). A full re-sort and re-filter is performed when this method is called, which is why the pagination reset is the default action.

Returns:

DataTables.Api instance

Examples

Reload the table data every 30 seconds:

var table = $('#example').DataTable( { ajax: "data.json" } ); setInterval( function () { table.ajax.reload(); }, 30000 );

Use the callback to update an external elements:

var table = $('#example').DataTable(); table.ajax.reload( function ( json ) { $('#myInput').val( json.lastInput ); } );

Related

The following options are directly related and may also be useful in your application development.