Datatables JS - HTML5 禁用特定列排序的方法?

Datatables JS - HTML5 Way To Disable Sorting On Specific Column(s)?

您知道如何使用 HTML5 来初始化 table 上的某些选项,而不是使用 Javascript 来完成相同的操作。一种方法是否比另一种更好不在这个问题的范围之内。

请参阅 HTML 5 data attributes To Set Options 获取更多信息


这个问题的目的是找到如何使用 HTML5 初始化禁用某些列的答案。

基本的 Javascript 初始化程序如下所示:

$(document).ready(function() {
$('#example').DataTable();

});


那么这就是你如何使用带有初始化函数的JS设置页面长度的例子:

  $('#example').dataTable( {
      "pageLength": 50
  } );

好的,更近一步了——这里有一种 JS 方法可以禁用第 1 列和第 3 列的排序,例如——在初始化函数中添加这一行:

 $('#example').dataTable( {
      "pageLength": 50,
      "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 1, 3 ] }]
  } );

如果您的 table 至少有 2 个列,则不会排序 table。

现在还有一种方法,例如使用 HTML5 属性数据为 table 设置页面长度(显示顺序...),例如这:

<table data-order='[[ 1, "asc" ]]' data-page-length='25'>
<thead>
    <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th data-class-name="priority">Salary</th>
    </tr>
</thead>



我的问题是:

如何使第 1 列和第 3 列不排序table 使用 HTML5 数据属性?

我已经尝试了所有我能想到的选项 - 这是我最后一次尝试

data-columnDefs="[{"bSortable": false, "Targets": [3]}]"



我在这里的事实表明我已经尝试了所有可能使用的资源,包括 Datatable JS Official Website、Google 和我最喜欢的 Whosebug,尽管它与某些资源有些相关问题,但无助于解决这个具体问题。

一如既往-我感谢你们每一位我的兄弟姐妹们在这些问题上的帮助和努力!!! Love & Piece Be With You All My People!!!

你们非常亲密。正确的 data- 属性如下所示:

<table id="example" class="display" data-column-defs='[{"sortable": false, "targets": [1,3]}]'>

根据 manual:

There are two important points to consider when using data-* attributes as initialization options:

  • jQuery will automatically convert from dashed strings to the camel case notation used by DataTables (e.g. use data-page-length for pageLength).
  • If using a string inside the attribute it must be in double quotes (and therefore the attribute as a whole in single quotes). This is another requirement of jQuery's due to the processing of JSON data data.

有关代码和演示,请参阅 this jsFiddle