16

Suppose i have the table like below :

And I want to disable sorting of Action Column

<!--index.html-->      
<table class="table table-striped table-bordered post-list-table" id="table" >
  <thead>                      
    <tr>
      <th>Title</th>
      <th>Created At</th>
      <th>Action</th>
    </tr>
  </thead>
</table>

<!--Script.js-->
$('#table').DataTable();
vishal ribdiya
  • 1,013
  • 2
  • 12
  • 20
  • Possible duplicate of [Jquery DataTables, sort by specific column?](https://stackoverflow.com/questions/7878098/jquery-datatables-sort-by-specific-column) – Manoz Nov 16 '17 at 05:17
  • [Disable sorting on last column when using jQuery DataTables](https://stackoverflow.com/questions/12008545/disable-sorting-on-last-column-when-using-jquery-datatables) – ᴍᴇʜᴏᴠ Jan 28 '19 at 12:39

5 Answers5

33

Try adding : columns.orderable

"columnDefs": [
    { "orderable": false, "targets": 2 }
  ]

JSFiddle Here

<!--Script.js-->
$('#table').DataTable( {
"columnDefs": [
    { "orderable": false, "targets": 2 }
  ]
  });
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table class="table table-striped table-bordered post-list-table" id="table" >
  <thead>                      
    <tr>
      <th>Title</th>
      <th>Created At</th>
      <th>Action</th>
    </tr>
  </thead>
</table>
Calvin Ananda
  • 1,440
  • 1
  • 14
  • 30
  • OK !! Working Fine Now, Thanks For Your Effort – vishal ribdiya Nov 16 '17 at 05:44
  • 1
    Apparently OP wanted to disable sorting on the last column, use `targets: -1` so that you don't have to hardcode the column number. [More info and full answer with an example here](https://stackoverflow.com/questions/12008545/disable-sorting-on-last-column-when-using-jquery-datatables/12008678#12008678). – ᴍᴇʜᴏᴠ Jan 28 '19 at 12:42
7

Do this in jQuery

var table = $('#tbl').DataTable({
            "columnDefs": [{ targets: 'no-sort', orderable: false }]});

and add a class 'no-sort' to whatever headers you want to disable sort like this..

<th class="no-sort">Header n</th>
Bharathi
  • 131
  • 1
  • 2
4

Add a class to columns which you want to disable sort

<th class="no-sort">Operations</th>

then add the following style to your css

table.datatable thead th.no-sort {
    background: none;
    pointer-events: none;
}
Hassan Alhaj
  • 333
  • 3
  • 11
2

Try this

$('#table').dataTable({
    // display everything
    "iDisplayLength": -1,
    "aoColumns":[
        {"bSortable": true},
        {"bSortable": true},
        {"bSortable": false}
    ]
});

for reference - https://stackoverflow.com/a/7878609/1982631

Manoz
  • 6,507
  • 13
  • 68
  • 114
0

If u want to disable sorting for all columns then u can do $(th).unbind(); in footable