Using fnRender with ajax source datatable

Using fnRender with ajax source datatable

jdrmjdrm Posts: 4Questions: 0Answers: 0
edited March 2012 in General
I was trying just modifying one of the examples to do customize a cell:

[code]var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "sources/deep.txt",
"aoColumns": [
{ "mDataProp": "engine" },
{ "fnRender": function( oObj ) {
return "Test";
} },
{ "mDataProp": "platform.inner" },
{ "mDataProp": "platform.details.0" },
{ "mDataProp": "platform.details.1" }
]
} ); [/code]
Which uses a source like:
[code]
{ "aaData": [
{
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": {
"inner": "Win 95+",
"details": [
"4",
"X"
]
}
},
...
...
[/code]
Data is displayed correctly but I started getting "DataTables warning (table id = 'example'): Requested unknown parameter '1' from the data source for row 0"

Anything I'm missing? Or I should be doing this in a different way?

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi jdrm,

    Thanks very much for taking up the DataTables support option!

    The problem that you are facing is that fnRender actually gives two parameters in DataTables 1.9, and the second parameter is the value of the cell. Using that value is by far the most common use of fnRender, and so its a bit of a shortcut - the "issue" with this however is that DataTables needs to get that value from somewhere, and without an mDataProp for that column it reverts to using array indexes (hence why the error mentions "parameter '1'").

    So the way to address this is to simply give the cell some default content using the sDefaultContent parameter. This will stop the error because when DataTables can't find a value for a cell it will request the default content - if there is no default content then we get the error you are seeing, otherwise it will use the default content. So what you want to do is:

    [code]
    var oTable = $('#example').dataTable( {
    "bProcessing": true,
    "sAjaxSource": "sources/deep.txt",
    "aoColumns": [
    { "mDataProp": "engine" },
    { "sDefaultContent": "",
    "fnRender": function( oObj ) {
    return "Test";
    } },
    { "mDataProp": "platform.inner" },
    { "mDataProp": "platform.details.0" },
    { "mDataProp": "platform.details.1" }
    ]
    } );
    [/code]

    Hope this explains the situation and makes the "fix" clear :-). Give me a shout if you have any further questions.

    Regards,
    Allan
  • jdrmjdrm Posts: 4Questions: 0Answers: 0
    Great thank you very much! I really think that the examples on fnRender reference should be updated ;)


    Regards,
    Jimmy Rivera
This discussion has been closed.