2014-11-20 62 views
1

我已升级到DataTables 1.10,并且无法使用column.data或column.render使用不同的值进行排序和显示。如何在DataTable中使用不同的值进行排序和显示1.10

的数据是这样的:

[ 
    { 
     "title":"Overview Report: (2014-07-12 11:49 - 2014-07-12 23:49)", 
     "reportDateRangeMilliseconds":43200000, 
     "DateRange":"12 hours" 
    }, 
    { 
     "title":"User Overview Report: (2014-07-12)", 
     "reportDateRangeMilliseconds":86400000, 
     "DateRange":"1 day" 
    }, 
    { 
     "title":"Activity Report: (2014-07-31 23:00 - 2014-08-03 00:00)", 
     "reportDateRangeMilliseconds":176400000, 
     "DateRange":"2 days, and 1 hour" 
    } 
] 

我想创建一个列显示DateRange和排序使用reportDateRangeMilliseconds

我已经试过:

$('#reportList').dataTable({ 
    "data" : reportData,  
    "columns" : [ 
     { "title" : "Report Name", 
     "data" : "title" 
     }, 
     { "title" : "Date Range", 
      "data" : "reportDateRangeMilliseconds", 
      "render" : { 
      "display" : "DateRange" 
      } 
     } 
    ] 
}) 

但它返回错误:

DataTables warning: table id=reportList - Requested unknown parameter 'reportDateRangeMilliseconds' for row 0. For more information about this error, please see http://datatables.net/tn/4

http://jsfiddle.net/scottglew/pmpj9uyb/1/

我也试过:

$('#reportList').dataTable({ 
    "data" : reportData,  
    "columns" : [ 
     { "title" : "Report Name", 
     "data" : "title" 
     }, 
     { "title" : "Date Range", 
      "data" : { 
       "sort" : "reportDateRangeMilliseconds", 
       "display" : "DateRange" 
      } 
     } 
    ] 
}) 

没有返回一个错误,但也不会正确地排序使用毫秒值。见http://jsfiddle.net/scottglew/jrnou3p3/2/

我也尝试了一系列的其他组合,但没有任何喜悦。任何人都可以拯救我的理智吗?

回答

3

我终于找到了一种方法来实现这一点,通过为毫秒值创建另一个隐藏列,然后我指出'日期范围'列的orderData属性为隐藏列。

$('#reportList').dataTable({ 
    "data" : reportData,  
    "columns" : [ 
     { "title" : "Report Name", 
     "data" : "title" 
     }, 
     { "title" : "Range In milliseconds", 
      "data" : "reportDateRangeMilliseconds", 
      "visible" : false 
     }, 
     { "title" : "Date Range", 
      "data" : "DateRange", 
      "orderData" : [1] 
     }, 
    ] 
}); 

看到http://jsfiddle.net/vxshL3ju/1/

但不这场失利新的“排序”和“显示”属性中的数据表1.10推出的目的是什么?

1

对于我在一个Rails应用程序一个HAML文件的需要,这是我如何实现的观看和人类可读的距离排序(英尺/近时米,英里/千米时> 1英里)经由一个隐藏的距离列:

$('.water_supply table').DataTable({ 
    "order": [[3, "asc"]], 
    "paging": true, 
    "pageLength": 20, 
    // Have the visible distance column (2) sort actually use the 
    // (hidden) monotonic distance_in_miles column data (3rd) 
    "columnDefs": [ 
     { "targets": [2], 
     "visible": true, 
     "orderData": [3] 
     }, 
     { "targets": [3], 
     "visible": false, 
     } 
    ] 
}); 
+0

这最终对我来说真的很有帮助,它比安装排序扩展要容易得多。 +1 – 2017-11-02 18:43:55

相关问题