使用格式化数据进行渲染,使用DataTables.net中的原始数据进行排序

Jpl*_*us2 4 javascript jquery datatables datatables-1.10

这是我的数据表配置的示例

{
    "dom"        : "rltip",
    "processing" : true,
    "serverSide" : false,
    "order"      : [ [ 1 , "desc" ] ],
    "searching"  : false,
    data: [
       { "column-a" : "Sample Data A" , "column-b" : 10 , "column-c" : "Blah Blah" },
       { "column-a" : "Sample Data B" , "column-b" : 5 , "column-c" : "Blah Blah" },
       { "column-a" : "Sample Data C" , "column-b" : 38 , "column-c" : "Blah Blah" }
    ],
    "columnDefs" : [
        {
            "targets"   : 0,
            "orderable" : false,
            "data"      : "column-a"
        },
        {
            "targets"   : 1,
            "orderable" : false,
            "data"      : "column-b"
        },
        {
            "targets"   : 2,
            "orderable" : true,
            "className" : "title",
            "data"      : "column-c"
        }
     ]
}
Run Code Online (Sandbox Code Playgroud)

我想格式化显示的数据,但是在排序和其他与后端相关的东西上,我想使用原始的未格式化数据。

重要提示:我必须在客户端(javascript)上执行此操作。

我已经在columnDefs上尝试过渲染函数回调,但是它似乎不起作用。

"render" : function( data , type , row ) {

    if ( type === "sort" )
        return data;

    // format data here
    return data; // This is a formatted data

}
Run Code Online (Sandbox Code Playgroud)

我所说的“似乎不起作用”是指排序已损坏,它将考虑格式化的数据,而不仅仅是原始数据。

我找到了这篇旧的相关文章,但似乎不再适用于datatables.net的较新版本

https://datatables.net/forums/discussion/8249/filtering-using-the-rendered-text-however-sorting-using-the-original-value

我正在使用版本1.10.15

Shi*_*fty 11

渲染功能与不同的值多次调用类型。如果将未格式化的数据仅设置为类型排序,则与排序相关的类型(如type)会遗漏其他数据。代替处理的情况下, 显示器和用于在返回任何其它值未格式化的数据类型

"render" : function( data , type , row ) {    
    if ( type === "display" )
    {
       // format data here
       return data; // This is a formatted data
    }    
    return data; // This is a unformatted data    
}
Run Code Online (Sandbox Code Playgroud)

  • 这是应该添加到手册中的内容,因为该软件的排序方法相当不透明。 (3认同)
  • 该死的,我几乎已经接近了,谢谢你的帮助,伙计,它成功了:) (2认同)
  • 很棒的解决方案!为我节省大量重构。谢谢! (2认同)