1

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) {...})
}

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I'm manually querying a local database, and I want to reload whenever the database changes. I think your solution will work; however, I decided to go another way, which is to have the ajax function reference a 'global' variable of the database. Then, whenever the DB changes, I just call dt.ajax.reload()
Keep in mind that this won't work unless you set the destroy option to true
1

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();
}

1 Comment

I understand. So, please, let us know if it works too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.