Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  180] [ 2]  / answers: 1 / hits: 17830  / 8 Years ago, wed, september 28, 2016, 12:00:00

Is there a way to determine if a variable is an initialized DataTable or not? I'm basically trying to do this:


if (isDataTable(variable)) {
// datatable ... do datatable stuff
} else {
// not a datatable... do other stuff
}

I assumed $.fn.dataTable.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 (EDIT: the creator of DataTables has since extended $.fn.dataTable.isDataTable() in this commit so that this is no longer true):


var table = $("#table").DataTable(); // Valid initialized DataTable
console.log($.fn.dataTable.isDataTable("#table")); // Returns true
console.log($.fn.dataTable.isDataTable(table)); // Returns false...why?

More From » jquery

 Answers
3

I asked this question to the creator of DataTables, and he suggested using instanceof:


if (table instanceof $.fn.dataTable.Api) {
// variable "table" is a valid initialized DataTable ... do datatable stuff
} else {
// variable "table" is not a datatable... do other stuff
}

This approach works exactly as needed. He also went on to extend $.fn.dataTable.isDataTable() in this commit so that it will allow variable inputs.


[#60562] Tuesday, September 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;