datatables 2 ajax change dilemma

datatables 2 ajax change dilemma

azureazure Posts: 4Questions: 0Answers: 0

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

  • kthorngrenkthorngren Posts: 21,298Questions: 26Answers: 4,944

    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 using deferRender.

    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().

    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 by ajax.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

  • azureazure Posts: 4Questions: 0Answers: 0

    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.

  • kthorngrenkthorngren Posts: 21,298Questions: 26Answers: 4,944

    @allan might provide other options but you could look for the sent draw parameter value. The initial load it will be 1. If its 1 then return an empty data set. Or if the ajax.data parameters being sent are empty in initial load return an empty data set.

    Kevin

  • azureazure Posts: 4Questions: 0Answers: 0

    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.

  • allanallan Posts: 63,441Questions: 1Answers: 10,459 Site admin

    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:

    As of DataTables 2, this option can be given as an empty string to indicate to DataTables that it is an Ajax sourced table, but not to make an Ajax request until a URL is given

    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

  • azureazure Posts: 4Questions: 0Answers: 0

    Hi Allan,

    That's great news! I appreciate the prompt response and thankful for the great product! Thanks.

Sign In or Register to comment.