最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if variable is an initialized DataTable? - Stack Overflow

programmeradmin2浏览0评论

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?

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?
Share Improve this question edited Apr 20, 2023 at 21:49 reformed asked Sep 28, 2016 at 19:54 reformedreformed 4,80012 gold badges67 silver badges93 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 11

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.

You don't need to pass an JQuery instance to isDataTable().

isDataTable() requires an ID.

For more information : isDataTable()

Please check this:

How to check if DataTables are initialized

1. If you want to check by the id of the data table follow:

$.fn.DataTable.isDataTable('#id_of_my_data_table');

2. If you want to check with a jQuery object follow this method:

method I:

dataTable instanceof $.fn.dataTable.Api

method II:

$.fn.DataTable.isDataTable('#' + dataTable.attr('id'));

Any other method is in above answers not working for me as of 2024-Feb. No idea why.

From Documentacion of DataTable:

https://datatables.net/reference/api/$.fn.dataTable.isDataTable()

This method provides the ability to check if a table (tag) node is already a DataTable or not. This can be useful to ensure that you don't re-initialise a table that is already a DataTable.

The issue with the accepted answer is that it will prove difficult to get a proper reference to the actual DataTable element without having initialized the DataTable in the first place.

Instead, the newer versions of DataTables lets you flexibly use the $.fn.dataTable.isDataTable function reliably.

Before initialization:

$.fn.dataTable.isDataTable("#my_table")
false
$.fn.dataTable.isDataTable($("#my_table"))
false
$.fn.dataTable.isDataTable($("#my_table").eq(0))
false
$.fn.dataTable.isDataTable($("#my_table").get(0))
false

After initialization:

$.fn.dataTable.isDataTable("#my_table")
true
$.fn.dataTable.isDataTable($("#my_table"))
true
$.fn.dataTable.isDataTable($("#my_table").eq(0))
true
$.fn.dataTable.isDataTable($("#my_table").get(0))
true
发布评论

评论列表(0)

  1. 暂无评论