Jquery Datatables列的呈现和排序

fly*_*123 22 javascript ajax jquery datatables jquery-datatables

我正在使用一个数据表,其中包含一列格式的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);
        }
}
Run Code Online (Sandbox Code Playgroud)

一旦我这样做,排序不再正常.我的印象是该render函数应仅影响数据的显示,但仍应根据该行数据对象的基础数据进行排序.这些是我尝试使用的文档(http://datatables.net/reference/option/columns.render).

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

fly*_*123 35

我想我明白了.我只需要告诉render函数只对"display"类型进行操作:

{
        "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;
        }
},
Run Code Online (Sandbox Code Playgroud)

  • 它位于[docs](http://datatables.net/reference/option/columns.render#function)中,但很容易错过:"此函数可能被多次调用". (2认同)