using the API on table created within ajax call

using the API on table created within ajax call

webguruwebguru Posts: 16Questions: 0Answers: 0
edited February 2011 in General
Im building a table (among other parts of a site) inside of an ajax call.

Everything is working perfectly except that since datatables is called within the success function, I can't reference it with click function later on since the oTable isn't at the global scope.

I found this at stackoverflow explaining how to pass a variable from inside of an ajax success function to the global scope, but it doesn't seem to work when trying to add or remove datatables rows.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    You can use the object retrieval that DataTable allows to get your DataTables object. For example:

    [code]
    $.getJSON( 'xhr.php', [], function (json) {
    $('#example').dataTable( ... );
    do_more_stuff();
    } );

    function do_more_stuff () {
    $('#example').dataTable().fnFilter('hello');
    // or
    var oTable = $('#example').dataTable()
    oTable.fnFilter('hello');
    }
    [/code]
    Or you could use a non-locally scoped variable:

    [code]
    var oTable;
    $.getJSON( 'xhr.php', [], function (json) {
    oTable = $('#example').dataTable( ... );
    do_more_stuff();
    } );

    function do_more_stuff () {
    oTable.fnFilter('hello');
    }
    [/code]
    Allan
  • webguruwebguru Posts: 16Questions: 0Answers: 0
    Perfect, that seems to do the trick. Thank you!
This discussion has been closed.