How to remove sorting option from DataTables?

2020-06-01 05:21发布

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?

8条回答
SAY GOODBYE
2楼-- · 2020-06-01 05:50

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

$(document).ready( function() {
    $('#example').dataTable({
        "aaSorting": []
    });
})
查看更多
Animai°情兽
3楼-- · 2020-06-01 05:53

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.

查看更多
虎瘦雄心在
4楼-- · 2020-06-01 05:56

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 ] }
    ]
}); 
查看更多
够拽才男人
5楼-- · 2020-06-01 05:56

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
});
查看更多
甜甜的少女心
6楼-- · 2020-06-01 05:57

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 }
            ]   
        } );
 })
查看更多
Deceive 欺骗
7楼-- · 2020-06-01 05:58

There are 2 things I can think of that you can try.

First, try setting "bSort" to false. Note that this will disable sorting all around, so there is no need to disable it on individual columns.

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

Second, try setting aaSorting to empty. Note that this would be good to try if you still want to allow some other column(s) to be sortable.

$('#jTable').dataTable({ "aaSorting" : [[]] });

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

Kashif Solangi

查看更多
登录 后发表回答