Uncaught TypeError: $.fn.DataTable.isDataTable is

2019-08-25 02:23发布

问题:

I am using a data table to display data from database and i can perform delete and edit actions.with below code i am able to fetch data from database and perform edit and delete. but the issue is when i delete particular row it is deleted from database but still it is displayed in data table. i made use of "table.destroy();"but it gives the following error. Uncaught TypeError: $.fn.DataTable.isDataTable is not a function.

code for datatable:

<script>
 var table;

                if($.fn.DataTable.isDataTable('#sun_project_table') ) {
            table=$('#sun_project_table').DataTable();
            table.destroy();
            //$('#category_table_body').empty();
        }

                table=$('#sun_project_table').DataTable({
                "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],   
                "iDisplayLength": <?php echo 10; ?>,    
                'bProcessing'    : true,
                'bServerSide'    : true,
                'sAjaxSource'    : "<?php echo base_url();?>index.php/Registrationc/displayinfo8",
                columns: [
                            { "name": "van_village_mapping.van_id", "data": "van_id" }, 
                            { "name": "van_village_mapping.village_id", "data": "village_id" },
                                                        { "name": "Actions", "data": "Actions" }
                        ],
                "columnDefs": [
                {
                }],
                "createdRow": function ( row, data, index ) {
                },
                'fnServerData': function(sSource, aoData, fnCallback)
                {
                    aoData.push();
                    $.ajax
                    ({
                        'dataType': 'json',
                        'type'    : 'POST',
                        'url'     : sSource,
                        'data'    : aoData,
                        'success' : fnCallback
                    });
                },  
                "oTableTools": {
                    "sSwfPath": "assets/media/swf/copy_csv_xls_pdf.swf",
                },
                "oLanguage": {
                    "sSearch": "Filter: "
                }
            });

                });
        </script>
        <script> 
               function delete_van_village_mapping($1){
                     $.ajax({
                              type:"POST",
                              url: "<?php echo base_url(); ?>index.php/Registrationc/delete_van_village_mapping1",
                               data:{van_village_mapping_id:$1},
                               success: function()
                               {
                                   alert('done!');

                                }
                           });
                     };
         </script>

回答1:

For your question, since you set table = $('#sun_project_table').DataTable({...}); when you initialize the table, thus if you want to check if the table exists before you destroy it, you can just do

if(table) {
   table.destroy();
   ...
}

But I agree with other comment, if you just want to refresh the table, you can just remove the row, or clear the table and fetch it again via ajax.