How do I add and extra parameter to mRender in jquery.dataTables.js (version 1.9)

How do I add and extra parameter to mRender in jquery.dataTables.js (version 1.9)

SairusSairus Posts: 4Questions: 0Answers: 0
edited February 2013 in DataTables 1.9
Greetings,
I am working on adding an extra parameter to mRender in the datatables plugin called sFormat which should be passed to oCol in order to format data dynamically coming from JSON. Right now I have the datatable populating with raw unformatted data. These are the functions that I have in my HTML page:
----------------DATATABLES INIT FUNCTIONS--------------------------------
[code]
function fnBuildTable(metadata, data){

var aoColumns = [];

for(var i=0;i< metadata.length; i++) {
var coldef = metadata[i];

var column = {
"mData": coldef.fieldName,
"sTitle": coldef.headerName,
"sClass": "right",
"sWidth": "8%",
"mRender": fnRender,
"sFormat": coldef.format
};
aoColumns.push(column);
}

function fnRender(data, type, full, sFormat){
function fnRender(data, type, full){
var oTable = $("#tableChain").dataTable();
var oSettings = oTable.fnSettings();
var sFormat = $.format.number(data, "#,##0.00");
return data;
};
[/code]
------------------------------------------------------------------------
Some columns will be cities and countries with hyperlinks so I formatted that in aoColumnDefs like this:
-----------------------DATATABLES INIT----------------------------------
[code]
"aoColumns": aoColumns,//CALLS ARRAY FROM ABOVE
"aoColumnDefs": [{
"bVisible": true, "aTargets": [1],
"fnRender":
function (o) {
var num = "";
num += o.aData.Geography;
num += " ";
return num;
},
}][/code]
-------------------------------------------------------------------------------------------------
AND this is the part in jquery.dataTables.js 1.9 that I am trying to add the parameter sFormat so that specific numeral values will get formatted accordingly.
----------------------DATATABLES 1.9 PLUGIN------------------------------------------------------
[code]
/* Cache the data get and set functions for speed */
var mRender = oCol.mRender ? _fnGetObjectDataFn( oCol.mRender ) : null;
var mData = _fnGetObjectDataFn( oCol.mData );
oCol.fnGetData = function (oData, sSpecific) {
var innerData = mData( oData, sSpecific );
var sFormat = $.format.number(innerData, "#,##0.00");//NEED TO PUT sFormat somewhere to format number data coming in from JSON

if ( oCol.mRender && (sSpecific && sSpecific !== '') )
{
return mRender( innerData, sSpecific, oData );
}
return innerData;
};
[/code]

Replies

  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    Sounds to me like you want an anonymous function for your rendering method. Perhaps something like:

    [code]
    mRender: function ( data, type, row ) {
    return fnFormat( data, type, row, '{formatting string}' );
    }
    [/code]

    Allan
  • SairusSairus Posts: 4Questions: 0Answers: 0
    Hi Allan,

    The problem is that I don't know the {formatting string} at compile time. It is being returned with the JSON from the server as meta data. I tried dynamically building the aoColumns array and reinitializing the table, but I was having problems with the scope of the mRender that I was supplying.

    I didn't see an easy way to get to the aoColumns or even get the column index from within the mRender function. That's what prompted me to look into modifying the Datatables source.

    Basically, what I'm trying to accomplish is to build the columns dynamically from meta data in the server response, very similar to meta data in a Java ResultSet. Any ideas would be greatly appreciated.
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    > I didn't see an easy way to get to the aoColumns or even get the column index from within the mRender function

    It isn't possible*. You'd need to, as you say, modify the source (*actually it is, you could use the 'data' option as a flag, but that is rather involved and probably best avoided at the moment).

    Basically, at the moment you need to look at dynamically building the columns object. It is unfortunate that JSON doesn't allow functions to be returned, as that's the limited factor for DataTables accepting aoColumns in the result.

    Allan
This discussion has been closed.