Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datatables: test if datatables plugin is initialized

I want to check if a table element with say, id="datatable" is datatables-initialized. Something like this:

if ($('#datatable').dataTable().initialized) {
  alert("initialized!");
}
else {
      alert("not initialized!");
    }

How can I do that? Thanks!

like image 597
frenzy Avatar asked Feb 18 '11 17:02

frenzy


People also ask

How to check if DataTable is initialized or not?

isDataTable() could be used, but this only works for table selectors, not for variables containing an initialized DataTable. This function is returning false for such variables: var table = $("#table"). DataTable(); // Valid initialized DataTable console.

How check DataTable is initialized or not in jquery?

isDataTable() method provides the ability to check if a table node is already a DataTable or not. It can be accessed at any time, even before any DataTable has been created on the page. isDataTable() is a function of $. fn.

What is FN DataTable ext search push?

fn. dataTable. ext.search . This is an array of functions (push your own onto it) which will will be run at table draw time to see if a particular row should be included or not. This example shows a search being performed on the age column in the data, based upon two inputs.


2 Answers

You can the fnIsDataTable function in jQuery datatable

var ex = document.getElementById('example');
if ( ! $.fn.DataTable.fnIsDataTable( ex ) ) {
  $(ex).dataTable();
}

You can find more information in api

like image 85
Moustafa Samir Avatar answered Sep 18 '22 12:09

Moustafa Samir


First, add a special class name when you're initializing datatables:

$('.datatable').not('.initialized').addClass('initialized').dataTable()

And now you can tell them apart by class name:

alert( $('#datatable').hasClass('initialized') )
like image 34
Alexey Lebedev Avatar answered Sep 20 '22 12:09

Alexey Lebedev