Is there a way to find that the table on the page is a dataTable?

I am looking for a way to find out if the tables in the dataTable page are or not? Is there an easy way to find it? Or maybe if I can get all dataTable objects.

+6
source share
4 answers
if($('table').parents('.dataTable_wrapper').length>= 1) { do something... } 

This is a bit of a hack, but there is still no familiar way to validate a data instance. This is what I just found out yesterday.

+2
source

There is a static method in the DataTables plugin, so you can check how:

 $('table').each(function() { // this method accepts the DOM node (table element) as parameter if ( $.fn.dataTable.fnIsDataTable(this) ) { // do your thing to the table } }); 
+11
source

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.

+2
source

I know this may happen for a while, since this question was sent, but since I asked the same question myself, I came to this solution from the DataTable website link .

Here's how to check if #example DataTable is or not. If not, initialize:

 if ( ! $.fn.DataTable.isDataTable( '#example' ) ) { $('#example').dataTable(); } 

Hope this helps someone!

0
source

Source: https://habr.com/ru/post/906435/


All Articles