Plugin Index Data

Plugin Index Data

osteenbergenosteenbergen Posts: 1Questions: 0Answers: 0
edited December 2011 in General
Hi,

Just a small plugin I wrote as I could not find one. Instead of using Ajax source for fetching the data I have a Javascript TaffyDB for caching objects. It can fire triggers upon insert/update or remove. But datatables doesn't work correctly when data is updated that is not displayed. You need to give it a row index or a TR element. This row index is not the DT_RowId, but an internal number. This plugin build a link between this number and any index field in your objects

[code]
jQuery.fn.dataTableExt.oApi.fnAddIndexData = function ( oSettings, aData, index){
/* Add the data */
if(!index)
index = "DT_RowId";
var iRow = oSettings.oApi._fnAddData( oSettings, aData );
//Build the index array if it doesn't exist
if(!oSettings.aiData)
oSettings.aiData = [];
oSettings.aiData[aData[index]] = iRow;
oSettings.oApi._fnReDraw( oSettings );
}

jQuery.fn.dataTableExt.oApi.fnGetRowFromIndex = function ( oSettings, aData, index){
/* Find the index */
if(!index)
index = "DT_RowId";
if(!oSettings.aiData)
return false;
return oSettings.aiData[aData[index]];
}
[/code]

Usage:
[code]
var obj = callbackobj.obj;
var index = 'id'
if(callbackobj.action == Cache.INSERT){
table.fnAddIndexData(obj, index);
} else if(callbackobj.action == Cache.UPDATE){
table.fnUpdate(obj, table.fnGetRowFromIndex(obj, index));
} else if(callbackobj.action == Cache.REMOVE){
table.fnDeleteRow(table.fnGetRowFromIndex(obj, index));
} else {
return false;
}
[/code]

By default it used the DT_RowId, but as shown you can use any other field in your data object!

Good luck with it,

Onno

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    That's awesome - thanks for sharing your code with us!

    Regards,
    Allan
This discussion has been closed.