Created
January 10, 2013 06:29
-
-
Save royalsflush/4499937 to your computer and use it in GitHub Desktop.
An addon for Datatables – adds a new row after row with index provided; based on Allan Jardine's code for fnAddTr (I kept his code and comments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.dataTableExt.oApi.fnAddTrAfter = function ( oSettings, nTr, prevRowIdx, bRedraw ) { | |
if ( typeof bRedraw == 'undefined' ) | |
{ | |
bRedraw = true; | |
} | |
var nTds = nTr.getElementsByTagName('td'); | |
if ( nTds.length != oSettings.aoColumns.length ) | |
{ | |
alert( 'Warning: not adding new TR - columns and TD elements must match' ); | |
return; | |
} | |
var aData = []; | |
for ( var i=0 ; i<nTds.length ; i++ ) | |
{ | |
aData.push( nTds[i].innerHTML ); | |
} | |
/* Add the data and then replace DataTable's generated TR with ours */ | |
var iIndex = this.oApi._fnAddData( oSettings, aData ); | |
nTr._DT_RowIndex = prevRowIdx+1; | |
oSettings.aoData[ iIndex ].nTr = nTr; | |
var tmpData = oSettings.aoData[iIndex]; | |
oSettings.aoData.splice(iIndex,1); | |
oSettings.aoData.splice(prevRowIdx+1,0,tmpData); | |
/* If you don't need the correct indexes, I recommend removing this part | |
since it's slow */ | |
for (var i=prevRowIdx+2; i<oSettings.aoData.length; i++) | |
oSettings.aoData[i].nTr._DT_RowIndex=i; | |
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice(); | |
if ( bRedraw ) | |
{ | |
this.oApi._fnReDraw( oSettings ); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment