Datatable 中的 orderData 和 target 一起

orderData and target together in Datatable

本例为多列排序

$('#example').dataTable( {
  "columnDefs": [
    { "orderData": [ 0, 1 ],    "targets": 0 },
    { "orderData": 0,           "targets": 1 },
    { "orderData": [ 2, 3, 4 ], "targets": 2 }
  ]
} );

"orderData": 0, "targets": 1 是什么意思?

示例取自此处:https://datatables.net/reference/option/columns.orderData

上下文:我想向现有数据表(在 BIRT 中)添加一个新列。但是当我更改代码时,排序箭头消失了。所以我不得不查找这个示例代码。

要记住的初始点:列索引是 zero-based - 因此,第 0 列是您在 table 中看到的第一列;第 1 列是第二列 - 依此类推。列索引是在首次创建数据 table 时分配的,基于它们在 HTML table(或 DataTable 本身)中定义的顺序。


具体例子:

"orderData": 0,           "targets": 1

表示当用户点击列索引 1 的标题(即 table 中的第二列)时,table 中的数据将按列 0 中的数据排序(即 table).

中的第一列

我发现交换它们更直观一些 - 从左到右阅读它们:

{ "targets": 1, "orderData": 0 }

因为orderData列表中只有一个值,所以不需要在数组中。

另一个例子:

{ "targets": 0, "orderData": [ 0, 1 ] }

这意味着:当我点击列索引 0 时,数据将按列索引 0 排序,然后 sub-sorted 按列索引 1 排序。这里两个值需要在一个数组中:[ ].

一个典型的用途是如果你想根据隐藏列中的数据对可见列进行排序 - 特别是当可见列中的数据不是按照你想要的方式自然排序时table .


当您向 table 添加更多列时,您可能需要相应地调整列索引号。