我使用的数据表包含一列格式为YYYY-MM-DD HH:MM:SS
的mysql时间戳。我的表设置为最初按此列排序。数据表正确地自动检测时间戳格式并进行适当排序。
我现在正在尝试更改此列的外观以使用户更友好,但不会影响它的排序方式。所以,我使用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)。
有没有人知道我如何根据实际时间戳排序,但显示更友好的用户日期?
答案 0 :(得分:35)
我想我明白了。我只需要告诉渲染功能只能在"显示"类型:
{
"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;
}
},
答案 1 :(得分:0)
如果它是服务器端数据表,那么你可以做的就是在传递之前编辑一个json
就像在新数据表中的ssp.class.php的开头一样(告诉我你是否使用旧的数据表)
注意:在新的数据表中,它是一个名为formatter的内置函数,它可以执行相同的操作,但您可以使用这样的自定义函数
if ($j == 6) {
if ($data[$i][$columns[$j]['db']] == 1) {
$data[$i][$columns[$j]['db']] = '<label class="btn-danger disabled btn">Stopped</label>';
} else {
$data[$i][$columns[$j]['db']] = '<label class="btn-success disabled btn">Running</label>';
}
这里我只是将我的数据库中的0和1编辑为标签停止和运行
你可以做一些像parse_date这样的事情并存储一个重新格式化的