【问题标题】:Render using formatted data, Sort using raw data in DataTables.net使用格式化数据进行渲染,使用 DataTables.net 中的原始数据进行排序
【发布时间】:2017-12-15 09:57:06
【问题描述】:

这是我的数据表配置示例

{
    "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"
        }
     ]
}

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

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

我已经尝试了 columnDefs 上的 render 函数回调,但它似乎不起作用。

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

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

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

}

我所说的“它似乎不起作用”的意思是排序被破坏了,它会考虑格式化数据,而不仅仅是原始数据。

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

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

我使用的是 1.10.15

版本

【问题讨论】:

    标签: javascript jquery datatables datatables-1.10


    【解决方案1】:

    render function 被多次调用,type 中有不同的值。如果您将未格式化的数据仅设置为 sort 类型,则会错过其他与排序相关的类型,例如 type。而是处理 type display 的情况,并为 type 中的任何其他值返回未格式化的数据。

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

    【讨论】:

    • 该死的,我差点接近了,谢谢你的帮助队友,它成功了:)
    • 很棒的解决方案!为我节省大量重构。谢谢!
    • 这是应该添加到手册中的内容,因为该软件的排序方法相当不透明。
    猜你喜欢
    • 2018-01-23
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2017-05-18
    相关资源
    最近更新 更多