如何禁用数据表中仅特定一列的排序功能?

16
假设我有如下表格:
我想要禁用“操作列”的排序。
<!--index.html-->      
<table class="table table-striped table-bordered post-list-table" id="table" >
  <thead>                      
    <tr>
      <th>Title</th>
      <th>Created At</th>
      <th>Action</th>
    </tr>
  </thead>
</table>

<!--Script.js-->
$('#table').DataTable();

可能是Jquery DataTables,按特定列排序?的重复问题。 - Manoz
使用jQuery DataTables时,禁用最后一列的排序 - ᴍᴇʜᴏᴠ
5个回答

33

试试添加:columns.orderable

"columnDefs": [
    { "orderable": false, "targets": 2 }
  ]

这里是JSFiddle

<!--Script.js-->
$('#table').DataTable( {
"columnDefs": [
    { "orderable": false, "targets": 2 }
  ]
  });
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table class="table table-striped table-bordered post-list-table" id="table" >
  <thead>                      
    <tr>
      <th>Title</th>
      <th>Created At</th>
      <th>Action</th>
    </tr>
  </thead>
</table>


好的!现在工作正常了,谢谢你的努力。 - vishal ribdiya
1
显然,OP想要禁用最后一列的排序,使用targets:-1,这样您就不必硬编码列号。更多信息和完整答案及示例请参见此处 - ᴍᴇʜᴏᴠ

7

使用jQuery完成此操作

var table = $('#tbl').DataTable({
            "columnDefs": [{ targets: 'no-sort', orderable: false }]});

并在您想禁用排序的任何标题中添加一个类名为“no-sort” ,如下所示..

<th class="no-sort">Header n</th>

4

给你想要禁用排序的列添加一个class

<th class="no-sort">Operations</th>

然后在你的CSS中添加以下样式:

table.datatable thead th.no-sort {
    background: none;
    pointer-events: none;
}

2

试试这个

$('#table').dataTable({
    // display everything
    "iDisplayLength": -1,
    "aoColumns":[
        {"bSortable": true},
        {"bSortable": true},
        {"bSortable": false}
    ]
});

供参考 - https://dev59.com/V2sz5IYBdhLWcg3weHqU#7878609

该链接提供了一种方法,可以在Java中使用正则表达式来替换字符串中的所有特殊字符。这是通过使用Java的replaceAll()方法和正则表达式来实现的。下面是一个示例代码片段,说明如何将所有特殊字符替换为空格:

String input = "This string contains special characters !@#$%";
String output = input.replaceAll("[^a-zA-Z0-9]", " ");

在上面的示例中,replaceAll()方法使用正则表达式"[^a-zA-Z0-9]"来匹配字符串中的所有非字母数字字符,并将它们替换为空格。


1
与旧版DataTables完美兼容。 - Mohd Abdul Mujib

0
如果您想禁用所有列的排序,则可以在footable中执行$(th).unbind();

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