Correct syntax for accessing data for fnRender processing

Correct syntax for accessing data for fnRender processing

glawrieglawrie Posts: 18Questions: 3Answers: 0
edited July 2011 in DataTables 1.8
Hopefully a simple question.

Want to use fnRender to replace a numeric value in a table with a string.

The numeric data is column 21 of an HTML table - so have the following code for the fnRender initialisation...

[code] var oTable = $('#weblinkstable').dataTable( {
"aoColumnDefs": [
...
{ "fnRender": function ( obj ) {
var sReturn = obj.aData[21];
sReturn = (sReturn==1 ? 'String1' : 'fail1');
sReturn = (sReturn==2 ? 'String2' : 'fail2');
sReturn = (sReturn==3 ? 'String3' : 'fail3');
sReturn = (sReturn==4 ? 'String4' : 'fail4');
sReturn = (sReturn==5 ? 'String5' : 'fail5');
return sReturn;
},
"aTargets": [ 21 ]}
[/code]
The code seems to work - but sets the column value to 'fail5' regardless of its actual value.

So I guess the issue is how sReturn is being set in the first place. Presumably obj.aData[21] is not the right thing to use...

So what would be the right syntax to use?

Thanks in advance etc.

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited July 2011
    [code]
    switch (obj.aData[21]) {
    case 1: return 'String1'; // if aData is strings, and it might be, use 'case "1": '
    case 2: return 'String2';
    // .. etc.

    }
    return 'Fail';
    [/code]
  • glawrieglawrie Posts: 18Questions: 3Answers: 0
    Bril - thanks for the reply. Worked! Neater code too ;-)
This discussion has been closed.