fnServerData

fnServerData

didiergmdidiergm Posts: 14Questions: 0Answers: 0
edited May 2011 in General
Hello all,
First I am more & more impressed by dataTables it really is a superb piece of code! Fairly simple to use once you get you head around it, fast & reliable ... nice one Sir.

Next a quick question, what is the recommended way to return no data from the fnServerData callback.

here is a code fragment from fnServerData

[code]
.....
data.push({name:'SEARCHOPTION', value:so});
data.push({name:'SEARCHVALUE', value:sv});
$.ajax({
"datatype": "json",
"type": "POST",
"global": false,
"url": url,
"data": data,
"success": callback,
"error": function (jqXHR, textStatus, errorThrown)
{
alert (errorThrown);
if (jqXHR.status == 401 )
{
$('#diallogout').click();
LoginLogout();
}
}
.....
[/code]

I need under some circumstances, the ajax call not to be fired, so I need to exit, but want to do this in a graceful way, with the waiting banner properly hidden ...

What is the recommended way ?

thanks in advance
Didier

Replies

  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hmmm - good question... There isn't really a way of doing this. i had expected that if fnServerData were triggered, then it would always go and get the data, then return it. So what you would probably need to do is use the internal methods to remove the processing display - although that might still leave the table in an undefined state...

    I'm wondering if it might be worth introducing a pre-draw function which could be used to prematurely stop the draw, as I know that this has come up before - I'm just slightly concerned about adding more and more configuration options!

    Allan
  • didiergmdidiergm Posts: 14Questions: 0Answers: 0
    I understand your concern about not over bloating dataTables; and on the other end fnServerData seems like the logical place to decide whether or not to go fetch the data;

    So what about setting a separate callback fonction which would return a dummy json resultset (same sEcho as the one send, iTotalRecord & iTotalDisplayRecords set to 0 and aaData to [];
    If this works, you would not need to modify dataTables at all, just explain the 2 questions at the end of this post

    [code]
    function dummycallback ()
    {
    setup a dummy array
    }
    [/code]

    and in fnServerData I could then do something like

    [code]
    ....

    var mycallback = (Logic_deciding_to_get_the_date) ? callback : dummycallback;

    $.ajax({
    "datatype": "json",
    "type": "POST",
    "global": false,
    "url": url,
    "data": data,
    "success": mycallback,
    ....
    [/code]

    Would that be silly ?
    Note that I am note sure where to get the sEcho value from not the exact parameters for the callback fonction, it is just an idea.

    Didier
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hi Didier,

    I had wondered about something like that as well - but I think it would probably need a fair amount of support code to do, in order to get it reliability working, for what is a fairly unusual use case.

    What I have done however is to commit a new feature to DataTables which allows a new callback function called fnPreDrawCallback - which is called at the very start of a draw. If your function returns false, then the draw is cancelled - otherwise it continues on. Hopefully this will be of some use to you.

    Regards,
    Allan
  • didiergmdidiergm Posts: 14Questions: 0Answers: 0
    edited May 2011
    woow, thanks a lot Allan, I'll try that immediately

    [edit] done and used the opportunity to have a go at 1.8 (current nightly) and so far I have seen no obvious regression and get the feeling that speed has improved

    I implemented the new option you commited and it works a treat.

    Didier
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Excellent - thanks good to hear :-). Thanks for the feedback.

    Allan
This discussion has been closed.