accessing DataTables from global scope

accessing DataTables from global scope

TwoMiceTwoMice Posts: 5Questions: 0Answers: 0
edited September 2010 in General
I have a need to call some DataTables API functions from a user-generated onClick event. Should be simple. However, I'm working with a framework that initializes a table inside of $().ready(), but then does not store the return value in any variable outside of that scope.

The stripped-down code example below illustrates my situation. You'll see that there are some parts of the code that I cannot change, because it comes in as part of the framework.

Given this situation, is there any way to access the DataTables API for this table through an onClick event?

Thanks,
TM

[code]





<!-- This script block is actually included from a library, so I can't change it. -->

$(document).ready(function() {
// I know if I remove the following line, I'll get what I want, but I can't edit this block.
var oTable;

oTable = $('table#table_id').dataTable();
} );


<!-- I can edit anything below this line, but not above. -->


// This variable is never assigned a value, of course.
var oTable;








NumberText




1-0Foo.


1-1Bar




Check version.
<!-- no surprise, oTable is undefined, so the above does not work -->


[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited September 2010
    Hi TM,

    You don't need a global scope variable in order to work with the API - it's possible to retrieve the object that has been initialised previous by simply calling the $().dataTable() function again on the same table.

    So for example:

    [code]

    // This variable is never assigned a value, of course.
    var oTable = $('#table_id').dataTable();
    ....

    [/code]
    You don't even need to assign it to a variable (although it is faster if you are going to use multiple API calls). For example:

    [code]

    // This variable is never assigned a value, of course.
    $('#table_id').dataTable().fnFilter("allan");
    ....

    [/code]
    Hope this helps.

    Edit: I should have said - this required DataTables 1.7.0 or newer ( http://datatables.net/new/1.7 ).

    Regards,
    Allan
  • TwoMiceTwoMice Posts: 5Questions: 0Answers: 0
    edited September 2010
    You wrote: "I should have said - this required DataTables 1.7.0 or newer"


    Oops. _I_ should have said - this framework is using DataTables 1.6.2.

    So you're saying that in DataTables 1.7.0 or newer, this would work:

    [code]
    Check version.
    [/code]

    And that in DataTables 1.6.2, it's no surprise that I get an alert() that says "DataTables warning: Unable to re-initialise DataTable. Please use the API to make any configuration changes required."

    So to make this work I'd either have to rewrite the code in my example, or upgrade to v. 1.7?

    - TM
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi TM,

    Yes - I'm afraid that's basically the size of it. There is no way to obtain the originally created object instance in 1.6.2 (unless it to stored in "user space"), since it is not something that is stored by DataTables. Something which was a bit of a pain, which is why it was added in 1.7!

    1.7 is almost fully backwards compatible with 1.6. Only a deleting rows and regex filtering have changed in the API: http://datatables.net/upgrade/1.7

    Regards,
    Allan
  • TwoMiceTwoMice Posts: 5Questions: 0Answers: 0
    Allan, thanks for your quick and clear answers. Very helpful.

    All the best,
    TM
This discussion has been closed.