Earliest pre-render Callback

Earliest pre-render Callback

billdanburybilldanbury Posts: 1Questions: 0Answers: 0
edited July 2011 in General
I am using DataTables with an external datasource returning in object pairings. This is needed to support multiple front-end views with the same data service. To accomplish this, I parse the aoColumns into an array of column names which are visible in the view, and inject that array back into fnSettings(). I then use this list to correlate the fnRowCallback rendering per TD index. The earliest effective callback I found where this and other initialization can occur seems to be fnPreDrawCallback. However, this callback is called twice (???) so I had to add a mutex to ensure the initialization only happens once per table.

Is there a more appropriate Init callback for this purpose? I've found that fnInitComplete actually triggers AFTER fnRowCallback, causing a race condition in my init.

[code]
"fnPreDrawCallback": function() {
oSettings=$(this).dataTable().fnSettings();
if(!oSettings.bTableSetupComplete) {
oSettings=$(this).dataTable().fnSettings();

oSettings.sInstanceViewName=oSettings.sInstance.split('_',2)[1];
trigger="";
switch(oSettings.sInstanceViewName) {
case "SiteList": trigger="s"; break;
case "EquipmentList": trigger="e"; break;
case "ServiceList": trigger="ck"; break;
}
oSettings.sInstanceTrigger=trigger;

cols=oSettings.aoColumns;
colList=new Array();
for(col in cols) {
me=cols[col];
if(me.bVisible)
colList.push(me.mDataProp);
}
oSettings.aColumnList=colList;

oSettings.bTableSetupComplete=true;
}
},
[/code]

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    [quote]billdanbury said: (fnPreDrawCallback) this callback is called twice[/quote]

    I presume you are loading your external data source through Ajax then? fnPreDrawCallback will fire once for each draw of the table - the first time is when the DataTable has no data and shows a 'Loading...' message, and the second time is when it actually has data.

    fnRowCallback is called as each row is drawn onto the page, and then fnInitComplete is called when the table has fully initialised with the data loaded.

    The earliest callback available to manipulate data is fnRender - as that is done as the data is read. Otherwise then fnPreDrawCallback is the easiest. If you want it before fnRender even, what you could do is have your own fnServerData function and pre-process the data before sending it on to the DataTables draw function.

    Allan
This discussion has been closed.