datatables 2 ajax change dilemma
datatables 2 ajax change dilemma
datatables 2 changes the way how ajax server side processing works: It will fetch the data from ajax call during datatables initilization. This has performance concerns like unneccessary server and database access if we want to encourage users to enter filters before loading the data(but not force them to enter filters).
I have tried different ways:
Use empty url for datatables initialization; use data:function(d){xxx} to send dynamic parameters and set the url using ajax.Url('xxx').load() api or first set url('xxx') and the use ajax.reload().
Nothing work because the dynammic parameters are not passed in after setting the new url.
This works fine in datatables 1.x because we don't need to change the url and it won't automatically call the ajax function during initialization.
In datatables 2, only if we don't change the url then the dynamic parameters will be passed in. But then we are facing the performance hit which I am trying to avoid.
In a dilemma now and hope there is a solution. Thanks.
Replies
Datatables 1.x has the
deferLoading
option which prevents the -option ajax
request on initialization. It was removed in 2.0. See if this thread helps with ideas. Not sure if you were usingdeferRender
.I created a simple test case to show the
ajax.data
parameters are sent after changing the URL:https://live.datatables.net/gafawoha/8/edit
Use the browser's network inspector to view the sent payload and you will see the
myKey: myValue
parameter added byajax.data
.Click the Reload button, the URL is changed. There will be an error as the new URL is returning object data. Look at the sent payload with this request and you will see the
myKey: myValue
parameter is sent.Maybe I'm not understanding the problem. Please provide more details, the Datatables config and other relevant code. Better is a link to your page or test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Hi Kevin,
Interesting. If you change the initial url to empty '' in your example, you will see that mykey is not passing when you click the reload button. Thanks.
@allan might provide other options but you could look for the sent
draw
parameter value. The initial load it will be1
. If its1
then return an empty data set. Or if theajax.data
parameters being sent are empty in initial load return an empty data set.Kevin
Hi Kevin,
Thanks for the info!
I would think even the initial url is empty, the following url('xxx').load() call should pass the parameters to keep it consistent. And this would address the problem causing by the change of behavior of deferLoading. Hope this can be addressed in future versioin. For now I think I have to rollback to 1.x version. Thanks.
I'm actually going to reinstate the
deferLoading
option for DataTables 2.1. It has already been committed and I'm aiming for a release in about two weeks time.In the meantime, a workaround would be as Kevin says, return an empty data set if
draw===1
.There is a note in the
ajax
documentation that says:I'm not sure I ever considered that with server-side processing, which I had assumed would always need an Ajax URL. I'll check what happens there, but I think it is undefined behaviour at the moment (i.e. I'm not sure!).
Allan
Hi Allan,
That's great news! I appreciate the prompt response and thankful for the great product! Thanks.