开发者

jQuery datatables: test if datatables plugin is initialized

开发者 https://www.devze.com 2023-02-11 03:37 出处:网络
I want to check if a table element with say, id=\"datatable\" is datatables-initialized. Something like this:

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 {
      开发者_如何学JAVAalert("not initialized!");
    }

How can I do that? Thanks!


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


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') )


I feel following is the right answer to this.

$(document).ready(function(){
    if (jQuery().dataTable) {
         // your code to do some detaul set ups 
    }
});

For example

$(document).ready(function(){
    if (jQuery().dataTable) {

            $.extend( $.fn.dataTable.defaults, {
                iDisplayLength : 200,
                aLengthMenu : [[100, 200, 300, -1], [100, 200, 300, "All"]],
            });
        }
});

By this way you if(jQuery().<libname>) should be able to check any library loaded or not.


Datatable has a method to check if an element has been initialized as a datatable or not - $.fn.DataTable.fnIsDataTable

tableElement = document.getElementById('your table ID');
$.fn.DataTable.fnIsDataTable(tableElement); // returns true or false


After you've called .dataTable() does it do anything to the table that makes it identifiable? i.e. does it add a new class "initialised" or something like that? If so, you could just loop through the items like so:

$('.datatable').each(
    function(index, element) {
        var _table = $(element);
        if (_table.hasClass('initialised')) {
            // Do stuff
        } else {
            // Do stuff
        }
    }
);

Apologies if this isn't what you mean. It's not clear in your question what "dataTable()" actually does.


I have used callback() function to do the same in my scenario. Thought of sharing this as an alternate approach

/* During Initialization */
var isTableInitialized = false;
$('#datatable').dataTable({/* your dataTable configurations*/},initializeDT());

/* Implement a callback function to set the value */
function initializeDT() {
    isTableInitialized = true;
}

Later in code..

/* Checking for Initialization is easier*/
if(isTableInitialized) {
    /* Do something here */
} else {
    /* Do something here */
}


This worked for me.

    $(document).ready(function() {
    if (jQuery().dataTable) {
        $.extend( $.fn.dataTable.defaults, {
            /*some default settings*/
        });
    }

    $('#myDataTable').DataTable();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消