6

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

This is my js code:

$('#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.

I think I dont get it right how to use fnrender proberly, especially when working with objects.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
SchurigH
  • 369
  • 2
  • 4
  • 12

1 Answers1

20

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

or

oObj.aData["link"]

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" }
    ],
} );
Luke
  • 764
  • 9
  • 11
  • Thank you. After a day with DataTables I figuered this out by myself. I forgot to post this here and close this topic. – SchurigH Jul 15 '11 at 14:07
  • Thank you so much for the suggestion of "oObj.aData.link" because I was trying to build mine from a million row pieces and trying to hide things... when I could just output the link as needed, then add it with fnRender. – gloomy.penguin May 17 '13 at 16:50
  • Hi, please I need an answer to similar problem, however, my aoColumns uses mData. How can I include this "fnRender": function( oObj ) { return '' + oObj.aData.ftitle + '';in one of the mData. Thanks. – Kunbi Oct 01 '14 at 16:40