fnRender and displaying other fields

fnRender and displaying other fields

motormouthmikemotormouthmike Posts: 7Questions: 0Answers: 0
edited April 2012 in General
Hello

I seem to have a problem using fnRender with mDataProp and wondered what the best way is to show rendered columns alongside other fields which are coming straight from the json string.

My string is something like this:

[code]
{"sEcho": 0,"iTotalRecords": 3801,"iTotalDisplayRecords": 3801,"aaData": [{

"type": "13(5)"
,"description": "13(5) test"
,"product": "abc"
,"testa": "test"
,"testb": ""
,"testc": "test"
,"testd": "test"
,"status": "test"
,"entry_id": "N/A"
}]
}
[/code]

I now want to build a link from this information for one field but use the other values. So I tried to define the columns I wished to use in each column usin mDataProp but when I do this it says I'm requested an unknown parameter at zero.

I've read some other threads and I can see that if I define a function each time I can use fnRender, however since the operation is quite simple, do I really need to do this? It seems like over kill.

Any thoughts or suggestions welcome.

Michael.

[code]

$('#example').dataTable(
{
"bProcessing":true,
"sAjaxSource": 'index.cfm?event=Data',
"aoColumns": [{"fnRender": function(oObj, val){
return '' + oObj.aData["testb"] + '';}
},

{"mDataProp": "product"},
{"mDataProp": "type"},
{"mDataProp": "testa"},
{"mDataProp": "testb"},
{"mDataProp": "testc"},
{"mDataProp": "testd"},
{"mDataProp": "status", bRegex:true},
{"mDataProp": "entry_id"}
],


"bAutoWidth": false,
"bServerSide":true,
"bJQueryUI": false,
"bDeferRendering":true,
"sPaginationType": "full_numbers",
"sDom": '<"top"ilp>rt<"bottom"ip><"clear">'
})


} );


[/code]

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > So I tried to define the columns I wished to use in each column usin mDataProp but when I do this it says I'm requested an unknown parameter at zero.

    The reason for this is the second parameter passed to fnRender - the value. DataTables needs to be able to read the value for the column to be able to pass it in, but that's not possible at the moment since there is no value assigned to it! So either use sDefaultContent: "", or just pick a property in the JSON and assign that to the column in the knowledge that the fnRender function will override that.

    Allan
  • motormouthmikemotormouthmike Posts: 7Questions: 0Answers: 0
    Hi Allan,

    How do I assign the value to the function though? Everything I try gives the error:

    DataTables warning (table id="example"): Requested unknown parameter '0' from the data source for row 0. Even if it then goes on to display the information correctly.

    I've tried:

    [code]
    {"fnRender": function(oObj,sVal){
    return '' + oObj.aData["Claim_type"] + '';
    }}

    {"fnRender": function(oObj,{"mDataProp": "product"}){
    return '' + oObj.aData["Claim_type"] + '';
    }}

    {"fnRender": function(oObj,product){
    return '' + oObj.aData["Claim_type"] + '';
    }}

    {"fnRender": function(oObj,sDefaultContent){
    return '' + oObj.aData["Claim_type"] + '';
    }}

    [/code]

    I realise I'm probably doing something stupid, but I can't find any examples of how a value from the json is passed in.

    Thanks in advance,
    M
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    As I say, just use sDefaultContent for the column and make it an empty string - then DataTables will pass that in as the value :-)

    Allan
  • motormouthmikemotormouthmike Posts: 7Questions: 0Answers: 0
    Can you give an example Allan please? As you can see I thought I'd tried that...
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    [code]
    {
    "sDefaultContent": "",
    "fnRender": function(oObj,sVal) {
    return '' + oObj.aData["Claim_type"] + '';
    }
    }
    [/code]

    Allan
  • motormouthmikemotormouthmike Posts: 7Questions: 0Answers: 0
    Got it! I see what you mean now. That works. This isn't clear in the documentation though. I suggest that something like this is added as I'm sure I'm not the only person who will do something like this.

    Thanks for your quick and prompt response.

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

    Good to hear that works for you. Good point about the documentation - I'll add that in and it will be up with the 1.9.2 release (as the documentation is tied to the releases of the software :-) ).

    Regards,
    Allan
This discussion has been closed.