DataTables Change the order of columns

Viewed 2013

I am printing to table with datatable colums with my order but the data table change the order, what can I do to abort that.

My code

                        $(document).ready( function () {
                                    $('#example').DataTable();
                                } );
                            </script>
3 Answers

If you want to disable sort globally you can use this code:

$(document).ready(function() {
    $('#dTable').dataTable( 
         {
         "bSort" : false
         } 
    );
});

Or if you want to set a default sort column back to what it was in the backend:

$(document).ready(function() {
     $('#dTable').DataTable( {
          "order": [[ 3, "desc" ]]
      } );
    );
});

Here is a working solution for your problem, use bsort attribute of DataTables plugin to disable default sorting

$(function(){
    $("#example").dataTable({
      "bSort" : false
    });
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">

<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<table id="example">
    <thead>
      <tr><th>Sites</th></tr>
    </thead>
    <tbody>
      <tr><td>SitePoint</td></tr>
      <tr><td>Learnable</td></tr>
      <tr><td>Flippa</td></tr>
    </tbody>
  </table>

Hope it helps you to solve your problem

$(document).ready( function () {
  $('#example').DataTable({
   ...,
   order: [0, 'asc'],
   ...
   });
} );

or arrange the list descended by putting 'desc' instead of 'asc'. The first value in array assigned by key 'order' is column index. for your possible first column 'id' it is 0.

And you are done.

Related