如何设置';targets';在列中动态定义数据表


How to Set 'targets' Dynamically in columnDefs Datatables

我想根据列名更改列排序。我正在使用

$(document).ready(function () {
  $('.datatable_tri').dataTable({
 'columnDefs': [ { 'type': 'file-size', 'targets': 1 }  ],
    'aaSorting': [],
    'iDisplayLength': 50
  });
});

对于许多数据表,我有3个或更多,我不想硬编码像0或1这样的目标的值…

我也试过使用

'columnDefs': [ { 'type': 'file-size', 'title': 'SIZE' } ], 

但它不起作用。

PS:SIZE是列标题

这是一把小提琴-->http://jsfiddle.net/v07uxf35/3/

aaSorting虽然仍然受支持,但它是旧版本DataTables 1.9.x的一个属性。新版本1.10.x使用order属性。

根据order属性和相应的columns.orderData属性的文档,两者都只需要列索引。

如果不想在targets中使用索引,请使用columns而不是columnDef。它使代码可读性更强,因为它不需要targets,只需要按列在表中出现的顺序列出列。