禁用jQuery DataTables中特定列的排序功能

164

我正在使用jQuery的DataTables插件来排序表格字段。我的问题是:如何禁用特定列的排序功能?我已经尝试了以下代码,但它没有起作用:

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

我还尝试了以下代码:

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

但是这仍然没有产生期望的结果。


1
我已经编辑了我的答案,并提供了一个选项,您可以在TH定义中设置禁用列。 - Paulo Fidalgo
使用CSS禁用按钮。请查看此页面。 https://datatables.net/forums/discussion/21164/disable-sorting-of-one-column#Comment_66660 - Eugine Joseph
您可能还想查看https://cbabhusal.wordpress.com/2015/05/20/jquery-datatables-turn-off-sorting-of-a-particular-column/。 - Shiva
23个回答

0

这里是答案!

targets 是列号,从0开始计数

$('#example').dataTable( {
  "columnDefs": [
    { "orderable": false, "targets": 0 }
  ]
} );

0

您可以直接在列上使用 .notsortable() 方法

 vm.dtOpt_product = DTOptionsBuilder.newOptions()
                .withOption('responsive', true)
        vm.dtOpt_product.withPaginationType('full_numbers');
        vm.dtOpt_product.withColumnFilter({
            aoColumns: [{
                    type: 'null'
                }, {
                    type: 'text',
                    bRegex: true,
                    bSmart: true
                }, {
                    type: 'text',
                    bRegex: true,
                    bSmart: true
                }, {
                    type: 'text',
                    bRegex: true,
                    bSmart: true
                }, {
                    type: 'select',
                    bRegex: false,
                    bSmart: true,
                    values: vm.dtProductTypes
                }]

        });

        vm.dtColDefs_product = [
            DTColumnDefBuilder.newColumnDef(0), DTColumnDefBuilder.newColumnDef(1),
            DTColumnDefBuilder.newColumnDef(2), DTColumnDefBuilder.newColumnDef(3).withClass('none'),
            DTColumnDefBuilder.newColumnDef(4), DTColumnDefBuilder.newColumnDef(5).notSortable()
        ];

-2
在表格的th中设置类“no-sort”,然后添加CSS样式: .no-sort { pointer-events: none !important; cursor: default !important;background-image: none !important; } 这样可以隐藏上下箭头并禁用表头事件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接