How to get the table DOM element behind an API instance?
How to get the table DOM element behind an API instance?
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
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
Perfect, thank you!