FnIsDataTable function always returns false
I am using datatable version 1.9.4. Using the datatable function fnIsDataTable I need to check if the data table is already initialized or not. But using the fnIsDataTable function always returns false. Below is my function:
function getData() {
if ( $.fn.DataTable.fnIsDataTable( "#example" ) ) {
$("#example").dataTable();
} else {
$("#example").dataTable( {
bLengthChange": false,
"bFilter": true,
"bInfo": false,
"bSort": false,
"iDisplayLength": 50
} );
}
}
Can anyone help me with this?
+3
source to share
2 answers
I found a solution. We need to get a reference to this table element using the javascript getElementById function, and then use this reference variable in the fnIsDataTable function to check its initialization. Like below:
function getData() {
var ex = document.getElementById("example");
if ( $.fn.DataTable.fnIsDataTable( ex ) ) {
$("#example").dataTable();
} else {
$("#example").dataTable( {
"bLengthChange": false,
"bFilter": true,
"bInfo": false,
"bSort": false,
"iDisplayLength": 50
} );
}
}
+3
source to share
Alternatively, since v1.10 you can simply use isDataTable
instead fnIsDataTable
.
function getData() {
if ( $.fn.DataTable.isDataTable( "#example" ) ) {
$("#example").dataTable();
} else {
$("#example").dataTable( {
"bLengthChange": false,
"bFilter": true,
"bInfo": false,
"bSort": false,
"iDisplayLength": 50
} );
}
}
See documentation: https://datatables.net/reference/api/%24.fn.dataTable.isDataTable ()
0
source to share