如何将数据表中columnDefs的目标定义为“last”?

pea*_*ove 1 jquery datatables target columndefinition

我的目标现在设置为第 5 列:

"columnDefs": [{
  "render": function (data, type, row) {
    return data;
  },
  "targets": 5
}],
Run Code Online (Sandbox Code Playgroud)

我想要做的是,我不想将其定位到特定数字,而是将其定位到最后一列。

我需要的是这样的:

"columnDefs": [{
  "render": function (data, type, row) {
    return data;
  },
  "targets": last
}],
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 5

根据文档,您可以提供一个整数,它targets指定要使用的列的索引。

columnDefs.targets选项提供 DataTables 所需的信息,应将列定义对象应用于表中的哪些列。

有可能:

  • 0 或正整数 - 从左侧计数的列索引
  • 负整数 - 从右侧计数的列索引
  • 字符串 - 类名称将在列的 TH 上匹配(没有前导 .)
  • 字符串“_all” - 所有列(即分配默认值)

注意第二点;您可以提供一个负整数来从右侧开始索引,因此-1将是最后一列。

"columnDefs": [{
  "render": function(data, type, row) {
    return data;
  },
  "targets": -1
}],
Run Code Online (Sandbox Code Playgroud)