Jillian
Jillian

Reputation: 113

DataTables reload custom ajax function

DataTables has 3 options for using ajax: url, ajax object, and custom function.

I'm using the custom function option, and I'm trying to figure out how to set a different ajax function after the DataTables creation.

I've seen ajax.reload, but this only seem to work with the url ajax option. Any idea how to reset a custom function and reload the data?


As an example, I'd like to be able to do this:

// Create DataTable
dt = $('#MyTable').DataTable({
   ajax: function(input, callback, settings) {...}
});

// Update Ajax with a new function call
function updateAjax() {
    // reload with new ajax function.
    dt.ajax.reload(function(input, callback, settings) {...})
}

Upvotes: 1

Views: 570

Answers (2)

Jillian
Jillian

Reputation: 113

I think tfidelis answer will work too, but I ended up going a different route:

// Define variable globally
var myGlobalState = ...;

// Create DataTable
var dt = $('#MyTable').DataTable({
  ajax: function(input, callback, settings) {
    doSomething(myGlobalState);
  }
});

// Update Ajax when myGlobalState changes
function updateMyGlobalState() {
  myGlobalState = ...;
  dt.ajax.reload();
}

Upvotes: 1

tfidelis
tfidelis

Reputation: 453

let me ask... why do you need to reset your ajax function? I think you will need that only when you create dataTable again. if is this case, just create it again with your new ajax function:

// Update Ajax with a new function call
function updateAjax() {
  dt = $('#MyTable').DataTable({
    ajax: function(input, callback, settings) {...}
    });
}

If it is not the case, could you explain the situation for me?

Upvotes: 1

Related Questions