【问题标题】:How to Disable sorting of only one specific column in Data tables?如何禁用数据表中仅一个特定列的排序?
【发布时间】:2018-04-29 12:59:46
【问题描述】:

假设我有如下表格:

我想禁用操作列

的排序
<!--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


【解决方案1】:

尝试添加: columns.orderable

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

JSFiddle Here

<!--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>

【讨论】:

【解决方案2】:

在 jQuery 中执行此操作

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

并添加一个类 'no-sort' 到任何你想像这样禁用排序的标题..

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

【讨论】:

    【解决方案3】:

    向要禁用排序的列添加一个类

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

    然后将以下样式添加到您的 css 中

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

    【讨论】:

      【解决方案4】:

      试试这个

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

      供参考 - https://stackoverflow.com/a/7878609/1982631

      【讨论】:

      • 适用于旧版 DataTables。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2019-12-21
        • 2018-09-07
        • 2017-10-25
        • 2020-07-12
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        • 2019-12-03
        • 1970-01-01
        相关资源
        最近更新 更多