3

我已升级到 DataTables 1.10,但无法使用 column.data 或 column.render 使用不同的值进行排序和显示。

数据如下:

[
    {
      "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 警告:表 id=reportList - 请求第 0 行的未知参数“reportDateRangeMilliseconds”。有关此错误的更多信息,请参阅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/

我还尝试了一系列其他组合,但没有任何乐趣。谁能拯救我的理智?

4

2 回答 2

5

我终于找到了一种方法来实现这一点,通过为毫秒值创建另一个隐藏列,然后我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/

但这不会破坏 DataTables 1.10 中引入的新“排序”和“显示”属性的目的吗?

于 2014-11-21T18:32:01.117 回答
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,
      }
    ]
});
于 2017-02-01T03:53:54.510 回答