How to destroy the first datatable initialization (DataTable inside modal)

I have a modal that displays a table. And I use a datatable plugin so that the data is searchable and sortable. It works correctly at first, but when I close the modal and click another link to the same modal, it displays an error. I found a solution to destroy a DataTable, and I put destroy()datatable before initializing, but then no data is displayed inside the table. If I placed it after initialization, it gave me an initialization error the second time I clicked the button. How can i solve this?

here is my code:

    $.ajax({
      url: "<?php echo site_url('admin/group/getMember')?>",
      type: 'POST',
      data: { 'groupID': id},
      dataType: 'JSON',
      success: function(result){
        $('#records_table tbody').empty();
        // $('#records_table').DataTable({
            // "bLengthChange": false,
            // "paging":false,
        // });
        $('.modal-header #hdrmsg').text(result[0].fname);
        var trHTML='';

         $.each(result, function (i, item) {
            trHTML += '<tr><td>' + item.fname + '</td><td>' + item.mname + '</td><td>' + item.lname + '</td></tr>';
        });
        $('#records_table tbody').append(trHTML);
        $('#records_table').DataTable({
            "bLengthChange": false,
            "paging":false,
        });
        $('#records_table').DataTable().fnDestroy();

      }

  });
+3
source share
2

dataTables - , paging .. , . , , ? , destroy: true:

var table = $('#records_table').DataTable({
   ...
   destroy : true
});

, , .

  • jQuery $('#records_table tbody').empty(); table.clear()?
  • jQuery $('#records_table tbody').append(trHTML); table.row.add([...])?

, , dataTable , , :

var table;
$('#modal').on('show.bs.modal', function() {
   $.ajax({
      url: url,
      dataType: 'JSON',
      success: function(response) {
         var response = $.parseJSON(response.contents);

         //clear the table, if it exists
         if (table) table.clear();

         //reinitialise the dataTable   
         table = $('#records_table').DataTable({
           destroy: true,
           bLengthChange: false,
           paging: false
         });

         $.each(response, function(i, item) {
           console.log("inserting", item);
           table.row.add([
             item.name,
             item.position
           ]).draw();
         });
       }
    });
});       

. → http://jsfiddle.net/bz958dxj/

, :

//global table object
table = $('#records_table').DataTable({
   bLengthChange: false,
   paging: false
});

$('#modal').on('show.bs.modal', function() {
   $.ajax({
      url: url,
      dataType: 'JSON',
      success: function(response) {
         var response = $.parseJSON(response.contents);

         //clear the table
         table.clear();

         //insert data 
         $.each(response, function(i, item) {
           console.log("inserting", item);
           table.row.add([
             item.name,
             item.position
           ]).draw();
         });
       }
    });
});   

demo → http://jsfiddle.net/8mjke9ua/

NB: assume bootstrap .modal-header .

NB ²: $.parseJSON(response.contents), , . , -, .

+9

datatable " 0 0 0 ". o

0