How to disable Datatable jquery sorting when it is created manually?

Asked

Viewed 129 times

0

I have a datatable created manually in View follows code

<table id="datatable">
  <thead>
    <tr><th>Nome</th></tr>
  </thead>
  <tbody>
     <tr><td>Victor</td></tr>
  </tbody>
</table>

It is a data table that uses the jquery plugin

How can I set "aaSorting" to: [], after it is loaded ?

Follow the code I try to make

$('#datatable').DataTable({
  "aaSorting" : [],
});

But I can’t because the datatable has already been created, and the . Datatable is only for data initialization

  • When is it created? Post more details.

1 answer

0

You cannot change the options after initialized, except destroying the component and rebooting with new options. See:

$(document).ready(function() {
   $('#datatable').DataTable(); // inicializa
   var opts = { "aaSorting": [] }; // insere a opção
   $('#datatable').DataTable().destroy(); // destrói
   $('#datatable').DataTable(opts); // reinicializa com as novas opções
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="datatable">
  <thead>
    <tr><th>Nome</th></tr>
  </thead>
  <tbody>
     <tr><td>Victor</td></tr>
     <tr><td>Bilbo</td></tr>
     <tr><td>André</td></tr>
     <tr><td>Caio</td></tr>
  </tbody>
</table>

Now, you have to insert into the object opts the existing options when the component was started, along with the new option aaSorting.

  • That is, I have to have the data I had in the json table to send dnv when it is rebooted ?

  • No, the data will already be on the table.

  • hahahahaha que tooooooop mano !!!!!!!!!!!!!!!!!!!!!!!!!!!!! I will test if it works you are top

  • You know how I paint a line from a datatable and appear painted when I click to print on the button (print) ?

  • It didn’t work, it turns out that the data table cannot be initialized because it already has an initialized ! My data table is not initialized with . Datatable() it is written in the hand

  • The answer code is just an example. Vc should only put the code from the line var opts = { "aaSorting": [] };.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.