jQuery DataTables fnrender与对象 [英] jQuery DataTables fnrender with objects
问题描述
我正在使用jQuery DataTable从这个data.txt中形成一个表:
I'm using jQuery DataTable to form a table out of this "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"
} ] }
这是我的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" }
],
} );
我只想使用.aData的 fnrender(),但这只适用于仅数组数据。
我现在得到的是undefined undefined,如果我使用.txt与数组数据,它可以正常工作。
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 proberly,特别是在处理对象时。
I think I dont get it right how to use fnrender proberly, especially when working with objects.
推荐答案
你正在获得undefined,因为oObj.aData是对象,而不是数组,并且没有0字段。使用这样的语法:
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.link
或
oObj.aData["link"]
完整示例(仅修改fnRender返回值):
Full example (only modified fnRender return value):
$('#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" }
],
} );
这篇关于jQuery DataTables fnrender与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!