这是我的数据表配置示例
{
"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
我正在使用版本 1.10.15
最佳答案
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
}
关于javascript - 使用格式化数据渲染,使用 DataTables.net 中的原始数据排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45032827/