How to get current table instance from a TableTools' button?

How to get current table instance from a TableTools' button?

icebergiceberg Posts: 14Questions: 0Answers: 0
edited May 2011 in TableTools
How to get current datatable instance from a TableTools' button?

Currently I have to pass the raw table id into the button, and then use jquery to dig it out.

sTableId: "blah",
fnClick: function(){
currentDataTableInstance = $(oConfig.sTableId).dataTable();
....
}

I think the cleaner way should be no sTableId needed, just:

currentDataTableInstance = this.fnGetDataTableInstance();


Ray

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    [code]
    this.s.dt.oInstance
    [/code]
    will give you the DataTables object that the TableTools instance belongs to. This isn't documented yet! Something that will get done when I transition TableTools to the new style of documented like FixedColumns :-)

    Allan
  • icebergiceberg Posts: 14Questions: 0Answers: 0
    Mmm, that is hard to remember. Hopefully we will have a "fnGetXxxxInstance()" api in future.
    By the way, another similar question, how to get current DataTables object from inside a fnRowCallback()?

    Thanks in advance.

    Ray
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Just use 'this'. All DataTables callback functions are called with the instance's execution scope.

    Allan
  • icebergiceberg Posts: 14Questions: 0Answers: 0
    edited May 2011
    "This" is intuitive! (why didn't I think "this" way? my bad)

    One funny point, though. It seems "this" does not always exactly the same as datatable instance. At least not for fnInitComplete().

    var oTable = $("#foo").dataTable({
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    console.log(this==oTable); //that will show many "false" in the console, but then many "true". Strange.
    this.fnWhatever(); // it works anyway
    return nRow;
    },
    "fnInitComplete": function () {
    alert(this==oTable); //that shows a "false",
    this.fnGetNodes(); // How ever, I can still do this. Strange, strange.
    }
    })

    I guess those "false" above is only because the variable oTable is not yet fully initialized. After all, as long as "this" works anyway, I am fine. :-)

    Ray
This discussion has been closed.