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.

1 Answer 1

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" }
    ],
} );
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. After a day with DataTables I figuered this out by myself. I forgot to post this here and close this topic.
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.
Hi, please I need an answer to similar problem, however, my aoColumns uses mData. How can I include this "fnRender": function( oObj ) { return '<a href="' + oObj.aData.link + '">' + oObj.aData.ftitle + '</a>';in one of the mData. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.