带有对象的 jQuery DataTables fnrender
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6518989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery DataTables fnrender with objects
提问by SchurigH
I'm using jQuery DataTable to form a table out of this "data.txt":
我正在使用 jQuery DataTable 从这个“data.txt”中形成一个表格:
{ "aaData" : [
{
"ftitle": "Test1",
"link": "http://server.com/test1/",
"fname": "test1.pdf",
"fid": "test1_353752165.pdf"
},
{
"ftitle": "Test2",
"link": "http://server.com/test2/",
"fname": "test2.pdf",
"fid": "test2_353754257.pdf"
} ] }
This is my js code:
这是我的js代码:
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "data/data.txt",
"aoColumns": [
{ "sClass": "center",
"fnRender": function( oObj ) {
return oObj.aData[0]+' '+ oObj.aData[2];
}
},
{ "mDataProp": "fid", "sClass": "center" },
{ "mDataProp": "fname", "sClass": "center" }
],
} );
I just want to get the actual data with .aData of fnrender()but this works only with array-only data. What I get now is "undefined undefined", if I use a .txt with just array data it works fine.
我只想使用 fnrender ()的.aData获取实际数据,但这仅适用于仅数组数据。我现在得到的是“未定义未定义”,如果我使用仅包含数组数据的 .txt 则它工作正常。
I think I dont get it right how to use fnrender proberly, especially when working with objects.
我想我不明白如何使用 fnrender 探针,尤其是在处理对象时。
回答by Luke
You're getting "undefined" because oObj.aData is an object, not an array, and there is no "0" field. Use syntax like this:
因为 oObj.aData 是一个对象,而不是一个数组,所以你得到“未定义”,并且没有“0”字段。使用这样的语法:
oObj.aData.link
or
或者
oObj.aData["link"]
Full example (only modified fnRender return value):
完整示例(仅修改 fnRender 返回值):
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "data/data.txt",
"aoColumns": [
{ "sClass": "center",
"fnRender": function( oObj ) {
return '<a href="' + oObj.aData.link + '">' + oObj.aData.ftitle + '</a>';
}
},
{ "mDataProp": "fid", "sClass": "center" },
{ "mDataProp": "fname", "sClass": "center" }
],
} );