ajax.reload() callback is being executed before the table is fully redrawn

ajax.reload() callback is being executed before the table is fully redrawn

AlviiAlvii Posts: 15Questions: 4Answers: 2

I have a table with some external filters, that needs to be redrawn every time a button is pressed. I also show the total rows that match a criteria in one column.

My problem is that, when i call ajax.reload(customFunction()), the customFunction, which updates the value based on the table data, is being executed before the table is redrawn.

Here is my code:

$('#search').on('click', function () {
   $.ajax({
      url: 'url,
      data: {
         data: data
      },
      success: function () {
         table.ajax.reload(customFunction()); 
      }
   });
})

customFunction () {
   showValue1();
   showValue2();
}

showValue1 () {
   var count = table.column(9).data().filter(//Criteria//).length;
   $('#show1').html(count);
}

The documents says:

callback: Function which is executed when the data has been reloaded and the table fully redrawn. The function is given a single parameter - the JSON data returned by the server, and expects no return.

But in my case, it is being executed before the table is redrawn.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948
    Answer ✓

    Here are a couple threads asking the same question:
    https://datatables.net/forums/discussion/comment/161168/#Comment_161168
    https://datatables.net/forums/discussion/comment/151119/#Comment_151119

    Sounds like the developers have asked for test cases showing the issue. Is that something you can do?

    Kevin

  • AlviiAlvii Posts: 15Questions: 4Answers: 2

    Thank you, I found the solution in the first thread you gave me.

    It seems that it only works if I wrap my customFunction inside function(){}.

    Here is my working code:

    table.ajax.reload(function () { customFunction()});
    

    For some reason, table.ajax.reload(customFunction()) doesn't work, but table.ajax.reload(function () { customFunction()}) does.

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948

    I think I see the problem with your original code. You are passing a function call which executes realtime. Instead you need to pass a reference to the function using either an anonymous function or the function object customFunction instead of customFunction(). I updated Colin's example from the first thread to show this.
    http://live.datatables.net/pamaneco/3/edit

    Kevin

This discussion has been closed.