Problem when using fnReloadAjax() together with sAjaxSource

Problem when using fnReloadAjax() together with sAjaxSource

kimkim Posts: 5Questions: 0Answers: 0
edited January 2010 in Plug-ins
Hi there,

I am a chinese developer who using DataTables recently.

The problem I met is that, when I set the table source to "sAjaxSource", then install fnReloadAjax plugin to refresh the data, it always make 2 ajax calls.

The code is something like this :

===============================================================================

[code]
var params = '?xxx=xxx';

$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback )
{
// content copied from http://datatables.net/plug-ins/api
}

......

$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "../examples_support/server_processing.php/" + params
} );
} );

......

oTable.fnReloadAjax("../examples_support/server_processing.php/" + params);
[/code]

===============================================================================

Then I go to firebug, and every time, it make 2 ajax calls.

"params" is dinamic which from user.

Did any of you had ever met this problem ?

Replies

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi kim,

    If you read the text which says what fnReloadAjax does it says: "Note: To reload data when using server-side processing, just use the built-in API function fnDraw rather than this plug-in."

    :-)

    Regards,
    Allan
  • kimkim Posts: 5Questions: 0Answers: 0
    Thank you allan !

    But another problem is I want to make the "params" dinamic, depending on some other input fields (for example).

    And if I use fnDraw, it just redraw the table, but never fresh the data by "params" provided.
  • kimkim Posts: 5Questions: 0Answers: 0
    edited January 2010
    Well, I make it works through this little plugin :
    [code]
    $.fn.dataTableExt.oApi.myReload= function ( oSettings, sNewSource )
    {
    if ( typeof sNewSource != 'undefined' ) {
    oSettings.sAjaxSource = sNewSource;
    }
    this.fnDraw( this );
    }
    [/code]
    oTable.myReload("../examples_support/server_processing.php/" + params);

    Can you give me your advice on whether it's right ? Or is there an easier way to do it ?

    Thank you.
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi kim,

    I suspect that is not quite right either. Have a look at this example for how to set your own custom variables to send to the server: http://datatables.net/examples/server_side/custom_vars.html

    Regards,
    Allan
  • kimkim Posts: 5Questions: 0Answers: 0
    Thank you allan!

    Yes, this function is really works well.

    But the problem of double ajax call still exists ... I aim at making every request to be only one ajax call ...
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    In the example I posted, it makes only one call for each request. So if you modify your code to more or less match that, then it should work quite nicely. Do you need to be able to change the sAjaxSource when you are using server-side processing (remembering that parameters should be added as shown in the example)?

    Allan
  • kimkim Posts: 5Questions: 0Answers: 0
    edited January 2010
    Allan, what I have to say is Thank You So Much.

    The example works so well now, and what's more, the fnCallBack is really what I am looking for so far.

    I make the ajax call in once and then do all the things with callback funciton. That's really nice.

    Here is my code :
    [code]
    oTable = $('.myTable').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "myAjaxGetData/",
    "fnServerData": function ( sSource, aoData, myCallback ) {
    aoData = myParamsCollection(aoData);
    $.getJSON(sSource, aoData, function (json) {
    myCallback(json);
    });
    },
    });
    [/code]
    Then just call fnDraw to refresh at any time I want.

    Thanks again for your patient !

    Kim
  • benayabenaya Posts: 1Questions: 0Answers: 0
    Hi Kim,

    thanks for the script, its working fine for me
  • diane simsdiane sims Posts: 2Questions: 0Answers: 0
    edited January 2011
    I would like to thank everyone for putting effort to solve this issue. I installed the application on my machine and found an unusual error similar to the one posted on this thread. I have tried to search for a solution on the internet but had no luck. I am glad I found this site and applied to method to resolve the issue. I am happy to announce that it worked like a charm. Thank you

    Diane Sims
    http://www.blenderreviewer.com/
This discussion has been closed.