Call fnDraw synchronously

Call fnDraw synchronously

adeelcap15adeelcap15 Posts: 4Questions: 0Answers: 0
edited December 2012 in General
hi allan,

I have following problem.
I am sending an ajax request (to server), and in response, i am refreshing the datatable using fnDraw. It was working fine until
I have a requirement to show a PDF. so after getting response from my ajax request, I am now refreshing the datatable and than
submiting a form to display a PDF.

Due to form submission, the fnDraw ajax request aborted, is there any way so that I call fnDraw method synchronously, and than POST the form.

Thanks

Replies

  • DukeAstarDukeAstar Posts: 11Questions: 0Answers: 0
    Why don't you use fnDrawCallback , this callback is call at the end of fnDraw function
  • adeelcap15adeelcap15 Posts: 4Questions: 0Answers: 0
    edited December 2012
    I don't want to use fnDrawCallback , as I am calling fnDraw from multiple places and in this scenerio, I only want to fire callback from one position, ofcourse I can use global variable to track, but i don't want to mess code. Another option is to register and unregister the callback ( fnDrawCallback ) event, is that makes sense?
  • DukeAstarDukeAstar Posts: 11Questions: 0Answers: 0
    if you don't want to use fnDrawCallback , you could instead modify the ajax query do disable the async function with fnServerData

    [code]
    $(document).ready( function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "xhr.php",
    "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    oSettings.jqXHR = $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "async": false,
    "success": fnCallback
    } );
    }
    } );
    } );
    [/code]
  • adeelcap15adeelcap15 Posts: 4Questions: 0Answers: 0
    edited December 2012
    Thanks for commenting. The above suggestion causes all ajax queries (table reload) to be synchronous. I just need synchronous functionality only at one place. any idea ?
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    fnDraw is synchronous in all aspects other than the Ajax fetch. If you want to make it synchronous for Ajax, you need to use something like DukeAstar suggested. If you only want it to be synchronous sometimes and asynchronous at others, you need to modify the fnServerData method you use to detect which situation it is being called in and change the `async` parameter as appropriate.

    Allan
  • adeelcap15adeelcap15 Posts: 4Questions: 0Answers: 0
    Thanks I did through async parameter and worked fine.
This discussion has been closed.