我正在使用一个数据表,该数据表包含格式为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;
}
},
好吧,如果它是服务器端数据表,那么您可以做的是像在新数据表中的ssp.class.php开头那样在传递JSON之前对其进行编辑(如果您使用的是旧的,请告诉我)
。在新的数据表中,它是一个称为格式化程序的内置函数,该函数可以执行相同的操作,但是您可以像这样使用自定义函数
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编辑为Label Stopped and Running,
您可以执行parse_date之类的操作并存储重新格式化的
本文地址:http://jquery.askforanswer.com/jquery-datatablesliedechengxianhepaixu.html
文章标签:ajax , datatables , javascript , jquery , jquery-datatables
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:ajax , datatables , javascript , jquery , jquery-datatables
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!