postpone table data population

postpone table data population

rgavrilovrgavrilov Posts: 13Questions: 6Answers: 0
edited January 2011 in General
I have a table placed on a dialog. At page start-up dialog is hidden. There is a chance user will never open it.
When dialog is shown, it also sets default filter settings, depending on how dialog was opened.

Can I initialize dataTable at the page load, but populate it only when dialog is opened?

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Where is the data coming from? If it's the DOM then no - it gets rendered when initialised. If from an Ajax source - sure just don't give it any data until you want to display it.

    Allan
  • rgavrilovrgavrilov Posts: 13Questions: 6Answers: 0
    I use server side - ajax data source:

    bServerSide: true,
    sAjaxSource: '../../../../Gems.aspx/Get',
    fnServerData: getDataTableServerData,

    what do you mean by "dont give it any data"?
    I want to prevent table from making a request. So ideally I would use an option "bDontGetDataOnInit" and then trigger data update with "fnGetDataNow" - figuratively speaking.

    For now my solution is to not call dataTable function until dialog opened first time and use a global flag "is table initialized".
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    edited February 2011
    I was going to suggest your current solution as one of the options - is this not good enough or causing some kind of problem?

    Another solution would be to make use of fnServerData to do something like this:

    [code]
    $(document).ready(function() {
    var flagInit = true;

    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../examples_support/server_processing.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    if ( flagInit ) {
    fnCallback({
    fnCallback({
    "sEcho": 1,
    "iTotalRecords": 0,
    "iTotalDisplayRecords": 0,
    "aaData": []
    });
    flagInit = false;
    });
    } else {
    aoData.push( { "name": "more_data", "value": "my_value" } );
    fnCallback(json);
    }
    }
    } );
    } );
    [/code]
    Allan
  • rgavrilovrgavrilov Posts: 13Questions: 6Answers: 0
    Global flags are evil. The state of the object shall not be put outside of the object. "Global" flag works, I just was hoping there is a more elegant solution, something like: Init->Load/Refresh
  • rgavrilovrgavrilov Posts: 13Questions: 6Answers: 0
    edited February 2011
    can I change fnServerData value on the fly? If I can then I can initialize it with:
    fnServerData : noResultDummyFunction
    and just set it to the correct value when I'm ready to show the table:
    $('#table').dataTable().fnSettings().fnServerData = realDataGettingFunction;
    will that work?
  • pdpipdpi Posts: 2Questions: 0Answers: 0
    rgavrilov, when you mentioned global flags, did you not notice that flagInit is declared locally in the document ready handler?

    The datatables fnServerData callback captures the flag in a closure, but that is not at all accessible from outside the closure, no global flags here at all!
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    There is another option if you want to avoid the external flag - there is one which is internal to DataTables: oSettings._bInitComplete . This is a private variable (as much as you get in Javascript), so it is possible it will change in future, but it's there at the moment :-)

    Allan
This discussion has been closed.