You can get all the tables on the page that are DataTables in one fell swoop. Code for DataTables 1.10:
$.fn.dataTable.tables()
Here is the doc . This will return an array of DOM elements. If you want to get the associated DataTable API instances, you can do:
var tables = $.fn.dataTable.tables(); var datatables = $(tables).DataTable();
Please note that although the call to .DataTable() , as described above, can create a new instance of the DataTable API and, therefore, initialize your table if the DOM element for which the method is called already have their own instances (the API is already created and initialized) , the call will not create new instances. In other words, the above code will not initialize your tables again.
The datatables object here is an instance of the API that manages the entire collection of tables. The API function call in this instance will control all tables at once. Adapting an example from the documentation, this code:
datatables.page('next').draw(false);
forces all tables to go to the next page (if it exists).
In versions prior to 1.10, the equivalent function was $.fn.dataTable.fnTables .
Roy Ling mentioned $.fn.dataTable.fnIsDataTable , which can be used to test tables separately. Of course, in 1.10, you can still test tables as needed, the $.fn.dataTable.isDataTable function.
Louis source share