How to check if a variable is an initialized DataTable?
How to check if a variable is an initialized DataTable?
It looks like $.fn.dataTable.isDataTable() works for selectors, but not for variables containing an initialized DataTable. I assumed this function could be used for this purpose, but it is returning false for such variables:
var table = $("#table").DataTable(); // Valid initialized DataTable
console.log($.fn.dataTable.isDataTable("#table")); // Returns true
console.log($.fn.dataTable.isDataTable(table)); // Returns false...why?
Why does this function return false? Is there a way to check variables to see if they are an initialized DataTable? How else can a variable be determined to be an initialized DataTable or not? I'm basically trying to do this:
// variable can be an initialized DataTable, string (selector), or jQuery object...
if (isDataTable(variable)) {
// datatable ... do datatable stuff
} else {
// not a datatable... do other stuff
}
Thanks!
This question has an accepted answers - jump to answer
Answers
You could just to
myVariable instanceof $.fn.dataTable.Api
, however, it does seem like a natural extension for the$.fn.dataTable.isDataTable
method - thanks for suggesting it! I've committed that change and it will be in the nightly builds shortly.Regards,
Allan
Awesome, thanks Allan!