禁用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个回答

5
如果您将第一列的orderable属性设置为false,则必须设置默认的order列,否则它将无法正常工作,因为第一列是默认排序列。下面的示例禁用了第一列的排序,但将第二列设置为默认的排序列(请记住,dataTables的索引是从零开始的)。
$('#example').dataTable( {
  "order": [[1, 'asc']],
  "columnDefs": [
    { "orderable": false, "targets": 0 }
  ]
} );

这是截至2020年7月17日对我有效的答案。 - Brian

4
为了更新Bhavish的答案(我认为这是针对DataTables的旧版本,并且对我没有用),我认为他们更改了属性名称。请尝试以下内容:
<thead>
    <tr>
        <td data-sortable="false">Requirements</td>
        <td data-sortable="false">Automated</td>
        <td>Created On</td>
    </tr>
</thead>
<tbody>
    <tr>
        <td>

这似乎是一个不错的方法...如果它能工作,但对我来说并没有。 这是否有文档记录? - AllInOne
1
@AllInOne:是的,对于 data-orderable...请参见 https://dev59.com/7m865IYBdhLWcg3wM7u0#32281113 。data-sortable 也可以使用,但我找不到它在哪里有文档记录。 - Jeromy French
更好的解决方案 - Washington Morais

3
"aoColumnDefs" : [   
{
  'bSortable' : false,  
  'aTargets' : [ 0 ]
}]

这里的0表示列的索引,如果你想让多列不排序,请列出列索引值,以逗号(,)分隔。


是否可以禁用所有列的排序? - fidel castro
是的,这是可能的。您可以访问此链接了解详情:https://cbabhusal.wordpress.com/2015/08/18/jquery-datatables-disable-sorting-for-all-columns/#more-1054 - Shiva
谢谢@coder,但是我无法从数据表格的第一列中删除排序功能,尽管我已经尝试使用0“零”和“-1”值。有什么建议吗?谢谢。 - Kamlesh

2

使用类:

<table  class="table table-datatable table-bordered" id="tableID">
    <thead>
        <tr>
            <th class="nosort"><input type="checkbox" id="checkAllreInvitation" /></th>
            <th class="sort-alpha">Employee name</th>
            <th class="sort-alpha">Send Date</th>
            <th class="sort-alpha">Sender</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" name="userUid[]" value="{user.uid}" id="checkAllreInvitation" class="checkItemre validate[required]" /></td>
            <td>Alexander Schwartz</td>
            <td>27.12.2015</td>
            <td>dummy@email.com</td>
        </tr>
    </tbody>
</table>
<script type="text/javascript">
    $(document).ready(function() {
        $('#tableID').DataTable({
            'iDisplayLength':100,
            "aaSorting": [[ 0, "asc" ]],
            'aoColumnDefs': [{
                'bSortable': false,
                'aTargets': ['nosort']
            }]
        });
    });
</script>

现在您可以给<TH>元素添加“nosort”类。

2
这对于单列来说对我很有效。
 $('#example').dataTable( {
"aoColumns": [
{ "bSortable": false 
 }]});

1
如果您已经需要隐藏某些列,比如我隐藏了姓氏列。我只需要连接名字和姓氏,所以我制作了查询,但从前端隐藏了该列。在这种情况下禁用排序的修改是:
    "aoColumnDefs": [
        { 'bSortable': false, 'aTargets': [ 3 ] },
        {
            "targets": [ 4 ],
            "visible": false,
            "searchable": true
        }
    ],

请注意,我在这里使用了隐藏功能。
    "columnDefs": [
            {
                "targets": [ 4 ],
                "visible": false,
                "searchable": true
            }
        ],

然后我将它合并到"aoColumnDefs"中。

谢谢@Pratik,但是我无法从数据表的第一列中删除排序功能,尽管我已经尝试了0“零”和“-1”值。有什么建议吗?谢谢。 - Kamlesh

1

有两种方法,一种是在定义表头时在HTML中定义。

<thead>
  <th data-orderable="false"></th>
</thead>

另一种方法是使用JavaScript,例如,您有一个表格

<table id="datatables">
    <thead>
        <tr>
            <th class="testid input">test id</th>
            <th class="testname input">test name</th>
    </thead>
</table>

然后,
$(document).ready(function() {
    $('#datatables').DataTable( {
        "columnDefs": [ {
            "targets": [ 0], // 0 indicates the first column you define in <thead>
            "searchable": false
        }
        , {
            // you can also use name to get the target column
            "targets": 'testid', // name is the class you define in <th>
            "searchable": false
        }
        ]
    }
    );
}
);

1
  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, 
    } );
    

0

代码将会长成这个样子:

$(".data-cash").each(function (index) {
  $(this).dataTable({
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
    "sPaginationType": "bootstrap",
    "oLanguage": {
      "sLengthMenu": "_MENU_ records per page",
      "oPaginate": {
        "sPrevious": "Prev",
        "sNext": "Next"
      }
    },
    "bSort": false,
    "aaSorting": []
  });
});

0

你也可以像这样使用负索引:

$('.datatable').dataTable({
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
    "sPaginationType": "bootstrap",
    "aoColumnDefs": [
        { 'bSortable': false, 'aTargets': [ -1 ] }
    ]
});

谢谢@Nurul,但是我无法从数据表的第一列中删除排序功能,尽管我已经尝试了0“零”和“-1”值。有什么建议吗?谢谢。 - Kamlesh

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