Jquery Datatables列呈现和排序

时间:2022-05-08 11:42:18

I'm working with a datatable that includes a column of mysql timestamps in the format YYYY-MM-DD HH:MM:SS. My table is set to initially sort by this column. Datatables correctly autodetects the timestamps format and sorts appropriately.

我正在使用一个datatable,其中包括一列以yyyyyyy -MM- dd:MM:SS格式的mysql时间戳。我的表将按本列初始排序。Datatables能够正确自动检测时间戳格式并进行适当的排序。

I'm now trying to alter the appearance of this column to be more user friendly, but not effect the way it gets sorted. So, I'm using the columns.render functionality like this:

我现在正在尝试修改本专栏的外观,使之更加用户友好,但不会影响它排序的方式。我用的是列向量。呈现这样的功能:

{
        "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);
        }
}

As soon as I do this, sorting no longer works correctly. I was under the impression that the render function should only effect the display of the data, but that it should still be sorted according to the underlying data on that row's data object. These are the docs I am trying to use (http://datatables.net/reference/option/columns.render).

一旦我这么做了,排序就不能正常工作了。我的印象是渲染函数只会影响数据的显示,但它仍然应该根据该行数据对象上的底层数据进行排序。这些是我正在尝试使用的文档(http://datatables.net/reference/option/columns.render)。

Does anyone know how I can sort based on the actual timestamp but display a more user friendly date?

有人知道我如何根据实际的时间戳进行排序,但显示一个更用户友好的日期吗?

2 个解决方案

#1


34  

I think I got it. I just had to tell the render function to only operate on "display" types:

我想我懂了。我只需要告诉渲染函数只对“显示”类型进行操作:

{
        "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;
        }
},

#2


0  

Well if it is a server-side datatable, what you can do is edit a json just before it is passed like in the beginning of ssp.class.php in the new datatables (tell me if you are using the old one)
NOTE: in the new datatables, it is an inbuilt function called formatter that does the same but you can use custom function like this

如果它是一个服务器端数据表,你可以做的就是在json像ssp.class开头那样被传递之前编辑它。注意:在新的datatable中,它是一个名为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>';
  }

here i am simply editing a 0 and 1 in my db to a Label Stopped and Running
you can do something like parse_date and store a reformatted one

这里我只是将db中的0和1编辑为已停止并运行的标签,您可以执行类似parse_date的操作并存储一个重新格式化的标签

#1


34  

I think I got it. I just had to tell the render function to only operate on "display" types:

我想我懂了。我只需要告诉渲染函数只对“显示”类型进行操作:

{
        "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;
        }
},

#2


0  

Well if it is a server-side datatable, what you can do is edit a json just before it is passed like in the beginning of ssp.class.php in the new datatables (tell me if you are using the old one)
NOTE: in the new datatables, it is an inbuilt function called formatter that does the same but you can use custom function like this

如果它是一个服务器端数据表,你可以做的就是在json像ssp.class开头那样被传递之前编辑它。注意:在新的datatable中,它是一个名为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>';
  }

here i am simply editing a 0 and 1 in my db to a Label Stopped and Running
you can do something like parse_date and store a reformatted one

这里我只是将db中的0和1编辑为已停止并运行的标签,您可以执行类似parse_date的操作并存储一个重新格式化的标签