jQuery datatables: test if datatables plugin is initialized

后端 未结 6 2354
猫巷女王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条回答
  •  闹比i
    闹比i (楼主)
    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().) should be able to check any library loaded or not.

提交回复
热议问题