1
votes

I have these two codes and one cancels the other. I need help getting the two together. Thanks

<script type="text/javascript" class="init">
  $(document).ready(function() {
    var table = $('#escritorios').DataTable({
      lengthChange: false,
      buttons: ['colvis', 'print', 'copy', 'excel', 'pdf']
    });

    table.buttons().container().appendTo('#example_wrapper .col-md-6:eq(0)');
  });
</script>

<script type="text/javascript">
  $(document).ready(function() {
    $('#escritorios').DataTable({
      responsive: {
        details: {
          type: 'column',
          target: 'tr'
        }
      },
      columnDefs: [{
        className: 'control',
        orderable: false,
        targets: 0
      }],
      order: [1, 'asc']
    });
  });
</script>
2
Generally we can't diagnose an error without seeing the relevant code. However in this case look at the URL in the error. It's caused when you've already initialised a DataTable on an element, and then later in your code do the same thing again. If you want more specific help, including how to change your code to avoid this problem, please edit the question to include that code. - Rory McCrossan
Thanks for editing the question. I added an answer for you below - Rory McCrossan

2 Answers

0
votes

Looks like you are trying to reinitialise a datatable with same ID. To reinitialise a datatabe you have to destroy it first.

if ($.fn.DataTable.isDataTable('#Table')) {
      $('#Table').DataTable().destroy();
}
0
votes

The issue is because you're initialising the DataTable() library on the same element multiple times, which is invalid, and causes the error you see.

To fix this combine all the settings in to a single call, like this:

jQuery(function($) {
  var table = $('#escritorios').DataTable({
    lengthChange: false,
    buttons: ['colvis', 'print', 'copy', 'excel', 'pdf'],
    responsive: {
      details: {
        type: 'column',
        target: 'tr'
      }
    },
    columnDefs: [{
      className: 'control',
      orderable: false,
      targets: 0
    }],
    order: [1, 'asc']
  });

  table.buttons().container().appendTo('#example_wrapper .col-md-6:eq(0)');
});