8

I am using jquery Datatable plugin. The init code is like below

$('#Table').dataTable( {
    "sAjaxSource": url
    ...
    ...
}); 

which gets fired on click of a button.Now on click of that button again I want to get the dataTable with a different url. I have tried using without success.Please suggest.

if (typeof obj == 'undefined') {

    obj = $('#Table').dataTable( {
    "sAjaxSource": url
    ...
    ...
    })
}else
{
    obj.fnClearTable(0);
    obj.fnDraw(false);

}
Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70

4 Answers4

10

I think what you need is fnReloadAjax() . You should use it like this:

var oTable = $('#Table').dataTable( {
    "sAjaxSource": url
    ...
    ...
}); 

var newUrl = "new.php";

oTable.fnReloadAjax(newUrl);
ktutnik
  • 6,882
  • 1
  • 29
  • 34
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • same problem your method not working for me, http://stackoverflow.com/questions/26246666/datatables-fnreloadajax-is-not-working-properly. I read the docs, i know it should work but i dont understand why it is not working for me.. – Sizzling Code Oct 07 '14 at 23:04
4

Try with this link: http://datatables.net/reference/api/ajax.url()

var table = $('#example').DataTable( { ajax: "data.json" } );
table.ajax.url( 'newData.json' ).load();

or as I did if table is not a dataTable object:

$('#tableId').DataTable().ajax.url("newUrl").load();

Matias
  • 928
  • 7
  • 5
2

This work for me:

var table = $('#Table').dataTable( {
    "sAjaxSource": url
    ...
    ...
}); 

url = 'newajax.php';

console.log('change input listened');
table.ajax.url(url);
table.draw();
Pande
  • 21
  • 1
1

This worked for version 1.10:

   oTable.ajax.url("new_source_file.php");
   oTable.draw();
kguest
  • 3,804
  • 3
  • 29
  • 31
major
  • 323
  • 1
  • 4
  • 11