您要做的第一件事是为您的日期列添加“columnDefs”对象并将其类型指定为“日期”。只要您遵循众所周知的格式,DateTables 就会内置日期解析。 ColumnType API Def
如果这不能让您完全到达那里,那么您将需要在您刚刚创建的新 columnDef 对象上为您的数据列定义一个渲染函数。在那里,您可以检查渲染类型并为其他所有内容返回一个“不错”的显示值和原始数据值(理想情况下是 Date 类型的值)。 Render API Defintion
还有一些一般性建议,不要试图与图书馆抗争。它实际上非常灵活,可以处理很多事情。所以尽可能使用内置的 API 函数。通常,当人们尝试使用 JQuery 手动操作表时,事情就会出错。在幕后,DataTables 插件维护了大量从未进入 DOM 的状态。基本上,如果 API 中有相应的函数,就使用它。
编辑:即使他找到了另一个解决方案,也要为原始发帖者的问题添加答案。
要记住的一点是,“过滤器”仅用于返回过滤后的数据集。它不会改变网格中的显示。相反,您将希望在“column()”API 项上使用“搜索”来过滤 DataTable 中显示的行。
然而,这有一个小问题。 search 方法只接受常规值而不接受函数。因此,如果您想实现这一点,您可以提供一个自定义搜索功能,如下所示:
// The $.fn.dataTable.ext.search array is shared amongst all DataTables and
// all columns and search filters are evaluated in the order in which they
// appear in
// the array until a boolean value is returned.
$.fn.dataTable.ext.search.unshift(function(settings, data, dataIndex) {
// Using a negative value to get the column wraps around to the end of
// the columns so "-1" will always be your last column.
var dateColumn = $(this).column(-1);
// We get the data index of the dateColumn and compare it to the index
// for the column currently being searched.
if(dateColumn.index() !== dataIndex) {'
// Pretty sure this indicates to skip this search filter
return null;
}
var columnSearchingBy = $(this).column(dataIndex);
// Allows the data to be a string, milliseconds, UTC string format ..etc
var columnCellData = new Date(data.lastModified);
var valueToSearchBy = new Date(columnSearchingBy.search.value);
// Ok this is one of the worst named methods in all of javascript.
// Doesn't actually return a meaningful time. Instead it returns the a
// numeric value for the number of milliseconds since ~ 1970 I think.
//
// Kind of like "ticks()" does in other languages except ticks are
// measured differently. The search filter I am applying here is to
// only show dates in the DataTable that have a lastModified after or
// equal the column search.
return (valueToSearchBy.getTime() >= columnCellData.getTime());
});
// So this should use our fancy new search function applied to our datetime
// column. This will filter the displayed values in the DataTable and from
// that just a small filter on the table to get all the data for the rows
// that satisfy the search filter.
var filteredData = table
.column({ data: "lastModified"})
.search('2015-10-10')
.draw();
即使您找到了另一种方法来解决这个问题,也许上述内容稍后会帮助您。