我正在使用包含 MySQL 时间戳列的数据表格,格式为 YYYY-MM-DD HH:MM:SS
。我的表格最初按照此列进行排序。Datatables 可以正确自动检测时间戳格式并进行适当的排序。
现在,我尝试更改此列的外观使其更加用户友好,但不影响其排序方式。因此,我使用 columns.render
功能,如下所示:
{
"data":"created_at",
"name":"date",
"visible":true,
"title":"Date Created",
"render": function(data, type, full, meta){
var date = new Date(data);
var options = {year: "numeric", month: "long", day: "numeric"};
return date.toLocaleDateString('en-US', options);
}
}
一旦我这样做,排序就不再正常工作。我原本以为render
函数只会影响数据的显示,但它仍然应该根据该行数据对象上的基础数据进行排序。我正在尝试使用的文档是 (http://datatables.net/reference/option/columns.render)。
有人知道如何根据实际时间戳进行排序,但显示更友好的日期吗?
type
有时候是"type"
,你希望在这种情况下返回初始的data
。按这种逻辑来做更有意义。 - Claudiu