我正在使用一个数据表,该数据表包含格式为mysql时间戳的列YYYY-MM-DD HH:MM:SS
。我的表设置为最初按此列排序。数据表可以正确地自动检测时间戳格式并进行适当排序。
我现在正在尝试更改此列的外观,以使其更加用户友好,但不影响其排序方式。因此,我正在使用如下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)。
有谁知道我可以如何根据实际时间戳进行排序,但显示出更加用户友好的日期?
我想我明白了。我只需要告诉render函数只对“显示”类型进行操作:
{
"data":"created_at",
"name":"date",
"visible":true,
"title":"Date Created",
"render": function(data, type, full, meta){
if(type == "display"){
var date = new Date(data);
var options = {year: "numeric", month: "long", day: "numeric"};
return date.toLocaleDateString('en-US', options);
}
return data;
}
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句