jQuery datatables: test if datatables plugin is initialized

后端 未结 6 2361
猫巷女王i
猫巷女王i 2021-02-13 00:34

I want to check if a table element with say, id=\"datatable\" is datatables-initialized. Something like this:

if ($(\'#datatable\').dataTable().initialized) {
           


        
相关标签:
6条回答
  • 2021-02-13 00:43

    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.

    0 讨论(0)
  • 2021-02-13 00:43

    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 */
    }
    
    0 讨论(0)
  • 2021-02-13 00:52

    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.

    0 讨论(0)
  • 2021-02-13 01:04

    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

    0 讨论(0)
  • 2021-02-13 01:05

    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
    
    0 讨论(0)
  • 2021-02-13 01:08

    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') )
    
    0 讨论(0)
提交回复
热议问题