Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload/refresh jQuery dataTable?

I am trying to implement functionality whereby clicking a button on the screen will cause my jQuery dataTable to refresh (as the server-side data source may have changed since the dataTable was created).

Here's what I have:

$(document).ready(function() {     $("#my-button").click(function() {         $("#my-datatable").dataTable().fnReloadAjax();     }); }); 

But when I run this, it does nothing. What's the proper way to refresh the dataTable when the button is clicked? Thanks in advance!

like image 478
IAmYourFaja Avatar asked Oct 17 '12 12:10

IAmYourFaja


People also ask

How do you refresh a data table?

Select Edit > Data Table Properties. Click to select the data table based on another data table and click Refresh Data > With Prompt.

How do you refresh a table in Javascript?

Syntax: object. refresh ( ); You can find the related objects in the Supported by objects section below.


2 Answers

With version 1.10.0 of DataTables, it is built-in and easy:

var table = $('#example').DataTable(); table.ajax.reload(); 

or just

$('#example').DataTable().ajax.reload(); 

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

like image 158
atmelino Avatar answered Sep 28 '22 01:09

atmelino


Destroy the datatable first and then draw the datatable.

$('#table1').DataTable().destroy(); $('#table1').find('tbody').append("<tr><td><value1></td><td><value1></td></tr>"); $('#table1').DataTable().draw(); 
like image 29
Sareesh Krishnan Avatar answered Sep 28 '22 01:09

Sareesh Krishnan