How to get the table DOM element behind an API instance?

How to get the table DOM element behind an API instance?

mgalgsmgalgs Posts: 7Questions: 4Answers: 0

I have some data tables that always live inside of a container which also contains some other useful things (like sorting controls) that I need to access from within a columns.render callback. I'm currently doing it like so:

"render": function(data, type, row, meta) {
    var api = new $.fn.dataTable.Api(meta.settings);
    var container = $(api.$("tr:eq(0)")).closest('div.my-table-container');
    // ...
}

This works fine but the whole tr:eq(0) business just feels a bit hacky... Is there a better way to get the table DOM element (which I could then use to get the container using $(table).closest('div.my-table-container'))?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin
    Answer ✓

    You can get the table container element that DataTables injects using the table().container() method.

    You might also be interested in using the static $.fn.dataTable.tables() method which can be used to get the tables on the page, including the visible tables, which is particularly useful when working with a tab like view.

    Regards,
    Allan

  • mgalgsmgalgs Posts: 7Questions: 4Answers: 0

    Perfect, thank you!

This discussion has been closed.