首页 > 解决方案 > 如何禁用对数据表中的列进行排序

问题描述

我正在尝试禁用我的一列上的排序功能。
我已经尝试了多种方法,但这些都不起作用。
我尝试添加这个: data-sorter="false"到我的<th>,但它只是忽略了它。
我也试过这个,但它也只是忽略了它:

“columnDefs”: [ {
“targets”: 2,
“orderable”: false
}]

当我尝试这些方法时,我没有收到任何错误。我还发现使用检查元素会<th>自动将类sorting添加到其中。
这是我的代码:
我的表:

<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
 <thead>
        <tr>
            <th>Vraag</th>
            <th>Gepost op</th>
            <th>Acties</th>// I want to disable sorting here
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

我的js:

// Call the dataTables jQuery plugin
$(document).ready(function() {
  $('#dataTable').DataTable({
    "columnDefs": [ {
      "targets": 2,
      "orderable": false
    } ]
  });
});

请帮助我,过去 3 天我一直在寻找答案。

标签: javascriptjqueryhtml-tabledatatable

解决方案


你试过设置 "bSort":false吗?有关更多详细信息,请参阅


禁用从数据表排序

"bSort":false


要禁用对特定列的排序:

"bSortable": false


更详细:

  $('#table').dataTable( {
    "bSort":true,
     aoColumnDefs: [
       { aTargets: [ '_all' ], bSortable: false },
       { aTargets: [ 0 ], bSortable: true },
       { aTargets: [ 1 ], bSortable: true }
    ]
  }

推荐阅读