vishal ribdiya
vishal ribdiya

Reputation: 1093

How to Disable sorting of only one specific column in Data tables?

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();

Upvotes: 16

Views: 39768

Answers (5)

Gurminder Kaler
Gurminder Kaler

Reputation: 101

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

Upvotes: 0

Bharathi
Bharathi

Reputation: 121

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>

Upvotes: 7

Hassan Alhaj
Hassan Alhaj

Reputation: 333

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;
}

Upvotes: 4

Calvin Ananda
Calvin Ananda

Reputation: 1530

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>

Upvotes: 33

Manoz
Manoz

Reputation: 6587

Try this

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

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

Upvotes: 2

Related Questions