"preDraw" event fired twice when using server-side processing
"preDraw" event fired twice when using server-side processing

Link to test case: https://datatables.net/examples/data_sources/server_side
Debugger code:
Execute this in the developer console:
$('#example').on('preDraw', function () { console.log('preDraw!'); });
Description of problem: When using server-side processing, "preDraw" is fired twice. The event order is as follows:
preDraw
preXhr
xhr
preDraw
draw
I did not expect the first "preDraw" and I'm struggling with it. Is that behaviour correct? I couldn't find anything regarding this.
Answers
This thread should help, it's asking the same thing. It's very old, back in the early days of DataTables, but the same logic still applies.
Cheers,
Colin
Thanks. I understand that this happens at the very first time, when the table is initialized (one time for an empty table, one time for the data from the server). But why is the event fired twice when there's already data in the table? The only thing that happens on the first time is the display of "Processing...", but the table is not drawn.
Another reason is that
preDraw
is cancellable - so it triggers first to give the option of being able to cancel before the Ajax call is triggred,Colin
Where is this useful? Wouldn't it make more sense to use
preXhr
to cancel an Ajax request?To be honest, I don't think it is particularly useful - it should perhaps be split out into two different events - one before the Ajax is triggered and one before the DOM is updated.
What's the use case here?
Allan
An extension of mine hides an element when the user changes the search filter or the page. I use
preDraw
for that.But there's an action where the table data is updated and nothing else should change. So I'm reverting the element hiding:
But while re-writing the code for composing this answer, I realised that I have to use
preXhr
because of the asynchronism instead. Because there's - of course - a(nother)preDraw
after "show", but nopreXhr
:So basically my problem is solved. But if you got an idea to improve the event system, this thread hasn't been for nothing. Thank you!