46

I'm using DataTables plugin. I don't want to use the sorting option (to sort the columns in ASC or DESC order) which comes by default on each <thead>. How can I remove that sorting icon?

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Does this answer your question? [Disable sorting for a particular column in jQuery DataTables](https://stackoverflow.com/questions/3932587/disable-sorting-for-a-particular-column-in-jquery-datatables) – Jeromy French Aug 24 '22 at 23:15

12 Answers12

113

In the new version 1.10 of jQuery DataTables you must use ordering option to disable ordering on the entire table:

$('#example').DataTable({
    "ordering": false
});
Kamlesh
  • 5,233
  • 39
  • 50
Ricardo Rivera Nieves
  • 1,305
  • 2
  • 8
  • 7
  • 3
    I cannot see in the documentation that ordering *must* be used globally and that ordering does not disappear if it is set individually to all columns. However, the answer seems to be correct. If I set each column to `"ordering": false`, then the sort symbol remains on the first column, even if it doesn't work. At least for me (DataTables Version 1.10.20). – colidyre Nov 22 '19 at 15:44
  • I confirm this works in `angular 2+` also. `dtOptions: DataTables.Settings {ordering: false,}` – Kishor Pawar Feb 11 '21 at 12:10
31

Very similar to @ravisolanki07 , it's just a different way to achieve this.

var oTable = $('#example').dataTable( {
    "aoColumnDefs": [
        { "bSortable": false, "aTargets": [ 0, 1, 2, 3 ] }, 
        { "bSearchable": false, "aTargets": [ 0, 1, 2, 3 ] }
    ]
}); 
Kiran Kanzar
  • 329
  • 1
  • 5
  • 14
Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71
21

You can also pass the option on the table itself using data attributes.

<table
   data-paging="false"
   data-searching="false"
   data-ordering="false"
>

The same principle applies to column headers.

<table>
<thead>
<tr>
    <th>I'm sortable</th>
    <th data-orderable="false">I'm not sortable</th>
</tr>
</thead>

But, I came across a situation where I wanted to remove all columns sorting and realize that Datatable still adds the icon on the first column when using a th data-orderable="false" on all columns, in that case, use the data-ordering on the table instead.

This can be handy should you use the same custom script to generate all your Datatables.

Julien B.
  • 3,023
  • 2
  • 18
  • 33
16

Ok, so, the answers here are a bit old. So I thought I could provide e newer answer:

source documentation

As of 2018, the way to achieve this, per field, is:

$('#id-of-my-table').DataTable({
    "columnDefs": [
        { "orderable": false, "targets": [0, 4, 5, 6] },
        { "orderable": true, "targets": [1, 2, 3] }
    ]
});

As you can see, targets accepts an array of column indexes.

Marco
  • 2,757
  • 1
  • 19
  • 24
13

If you want to disable the default sorting but keep the columns sortable, just use the following configuration :

$(document).ready( function() {
    $('#example').dataTable({
        "aaSorting": []
    });
})
tduchateau
  • 4,351
  • 2
  • 29
  • 40
7

There are 2 ways you can try.

First, try setting "bSort" to false. Note that, this will disable sorting all around.

$('#jTable').dataTable({ "bSort" : false } );

Second, try setting aaSorting to empty. Note that, this will remove sorting for specific column. $('#jTable').dataTable({ "aaSorting" : [[]] });

Let us know if either works for you. Hope it helps,

Kashif Solangi

Kashif Solangi
  • 892
  • 8
  • 12
3

Using the aoColumns attribute, sorting a specific column can be easily controlled. An example is given bellow:

$(document).ready(function() {
oTable = jQuery('#DataTables_Table_0').dataTable( {           
            "bDestroy": true,
            "bAutoWidth": true,  
            "bFilter": true,
            "bSort": true, 
            "aaSorting": [[0]],         
            "aoColumns": [
                { "bSortable": false },
                { "bSortable": true },
                { "bSortable": true },
                { "bSortable": true },
                { "bSortable": true },
                { "bSortable": true },
                { "bSortable": false }
            ]   
        } );
 })
Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
Arzon Barua
  • 494
  • 7
  • 11
1

-> for removing sorting in particular column then use orderable: false

-> for removing search from particular column then use searchable: false

 $('#table-name').DataTable({
            "columns": [
                   {"data": "column_name"},
                   {"data": "column_name" , orderable: false},
                   {"data": "column_name"},
                   {"data": "column_name" , orderable: false},
                   {"data": "action"}
                  ],
                  aoColumnDefs: [
                    {
                       bSortable: false,
                       aTargets: [ -1 ]
                    }
                 ]
      });
0

You can set it by bSortable to false in aocolumn like :

$('#example').dataTable({
    "aoColumns": [
        { "sType": "html","bSortable": false, "bSearchable": false },
        { "sType": "html" },
        { "sType": "html", "bSortable": false, "bSearchable": false },
        { "sType": "html" },
        { "sType": "html","bSortable": false, "bSearchable": false },
        { "sType": "html" },
        { "sType": "html" },
        { "sType": "html" },
        { "sType": "date-euro" }
    ]
});

You can also exclude from search by set bSearchable to false

Conrad37
  • 166
  • 1
  • 17
ravisolanki07
  • 637
  • 3
  • 13
0

Old question but none of these answers worked for me since none of them prevented the sort on click and I didn't want to reinitialize, so I'll post my solution:

Basically, clone the header, removing the sorting class from the <th> cells, insert clone after real header and hide the original. When you want to reenable, just reshow original header and remove clone.

This worked for my case, you may have to adjust the thead selector you use depending on your setup.

// grab the header
const stashedHeader = $('.dataTable thead');
// copy it and remove sorting class from the copy's th cells
const nosortClone = stashedHeader.clone();
nosortClone.find('th').removeClass('sorting');
// hide original and insert clone after it
stashedHeader.hide().after(nosortClone);

...
// re-enable
stashedHeader.show()
nosortClone.remove();
Anne B
  • 1
  • 1
0

Most of the other answers only deal with initial settings. I needed to disable sorting afterward via javascript based on user action.

If you pass in options to the DataTable function then it will fail because it has already been initialized. If you don't pass a parameter, then it will retrieve the current settings, and you can manipulate the existing settings.

$('#example').DataTable().settings()[0].aoColumns.forEach(function(c) { 
    // could use c.nTh to get the actual th element if needed
    c.bSortable = false;
});
kevinpo
  • 1,853
  • 19
  • 20
-1

Simply you can use this

"ordering": false,

Full Code:

$('#example').DataTable({
    "ordering": false,
 });
m4n0
  • 29,823
  • 27
  • 76
  • 89
Ridoy Paul
  • 49
  • 4