0

I have 2 datatables on the same page. I am using reload to reload them both as such:

setInterval( function () {
    jQuery('#tablepress-1').DataTable().ajax.reload();
    jQuery('#tablepress-2').DataTable().ajax.reload();
}, 30000 );

but this causes 2 calls ajax calls to the server. Server is already returning data for both tables in both calls. How to combine multiple reloads so that only 1 call is made to update both?

1 Answer 1

2
  1. Create a JSON call
  2. and send both the content in a single ajax call.
  3. On success, get both of the data and initialise the data table.

Example:

// Use the Fetch Api (no library)
fetch('/your/path').then(function(data) {
      $('.table1').html(data.table1);
      $('.table2').html(data.table2);
      //now initialise the tables
});

// Or use jQuery (better support)
$.ajax({
  method: 'POST',
  url: '/your/path',
  data: {}
})
  .done(function( data ) {
    $('.table1').html(data.table1);
    $('.table2').html(data.table2);
  });
Sign up to request clarification or add additional context in comments.

3 Comments

great answer! Could be improved with some example code :)
//some rough sketch
Fetch(yourpath).then(function(data){ $('.table1'). html (data.table1); $('table2').html(data.table2); //now initialise the tables });//rought sketch, send from mobile.

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.