disable a column sorting using jquery datatables

2019-01-08 06:31发布

I am using the jQuery datatables plugin to sort the table fields. My question is how do I disable sorting for a particular column? I have tried with the following code, but it did not work:

"aoColumns": [
    { "bSearchable": false },
    null
]   

I have also tried the following code:

"aoColumnDefs": [
     { "bSearchable": false, "aTargets": [ 1 ] }
 ]

but this still did not produce the desired results.

23条回答
放我归山
2楼-- · 2019-01-08 07:00

you can also use negative index like this:

$('.datatable').dataTable({
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
    "sPaginationType": "bootstrap",
    "aoColumnDefs": [
        { 'bSortable': false, 'aTargets': [ -1 ] }
    ]
});
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-08 07:01
  1. Use the following code to disable ordering on first column:

    $('#example').dataTable( {
      "columnDefs": [
        { "orderable": false, "targets": 0 }
      ]
    } );
    
  2. To disable default ordering, you can also use:

    $('#example').dataTable( {
         "ordering": false, 
    } );
    
查看更多
【Aperson】
4楼-- · 2019-01-08 07:02

Here is the answer !

targets is the column number, it starts from 0

$('#example').dataTable( {
  "columnDefs": [
    { "orderable": false, "targets": 0 }
  ]
} );
查看更多
孤傲高冷的网名
5楼-- · 2019-01-08 07:02

As of 1.10.5, simply include

'orderable: false'

in columnDefs and target your column with

'targets: [0,1]'

Table should like like:

var table = $('#data-tables').DataTable({
    columnDefs: [{
        targets: [0],
        orderable: false
    }]
});
查看更多
The star\"
6楼-- · 2019-01-08 07:05

set class "no-sort" in th of the table then add css .no-sort { pointer-events: none !important; cursor: default !important;background-image: none !important; } by this it will hide the arrow updown and disble event in the head.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-08 07:05

Here is what you can use to disable certain column to be disabled:

 $('#tableId').dataTable({           
            "columns": [
                { "data": "id"},
                { "data": "sampleSortableColumn" },
                { "data": "otherSortableColumn" },
                { "data": "unsortableColumn", "orderable": false}
           ]
});

So all you have to do is add the "orderable": false to the column you don't want to be sortable.

查看更多
登录 后发表回答