我正在使用以下方法获得职位:
this.fnGetPosition = function( nNode )
{
var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
var sNodeName = nNode.nodeName.toUpperCase();
if ( sNodeName == "TR" )
{
return _fnNodeToDataIndex(oSettings, nNode);
}
else if ( sNodeName == "TD" || sNodeName == "TH" )
{
var iDataIndex = _fnNodeToDataIndex( oSettings, nNode.parentNode );
var iColumnIndex = _fnNodeToColumnIndex( oSettings, iDataIndex, nNode );
return [ iDataIndex, _fnColumnIndexToVisible(oSettings, iColumnIndex ), iColumnIndex ];
}
return null;
};
如果我单击第 0 行,iDataIndex 包含 0,而 iColumnIndex 包含我单击的任何列。但是如果我添加一个新行,比如说第 9 行,然后我单击第 0 行,iDataIndex 仍然包含 9 而不是 0,并且 iColumnIndex 返回 - 1. 添加新行时我必须关闭 bServerSide,因为如果我不这样做,单击添加新行按钮不会添加空白行,而是进行 ajax 调用。添加行后,我重新打开 bServerSide。
fnNodeToDataIndex
看起来像这样:
function _fnNodeToDataIndex( oSettings, n )
{
return (n._DT_RowIndex!==undefined) ? n._DT_RowIndex : null;
}
fnNodeToColumnIndex
看起来像这样:
function _fnNodeToColumnIndex( oSettings, iRow, n )
{
var anCells = _fnGetTdNodes( oSettings, iRow );
for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
{
if ( anCells[i] === n )
{
return i;
}
}
return -1;
}
这是我想添加新行时调用的函数:
/* Add single row and display */
$.fn.dataTableExt.oApi.fnAddDataAndDisplay = function(oSettings, aData) {
/* Add the data */
var iAdded = this.oApi._fnAddData(oSettings, aData);
var nAdded = oSettings.aoData[iAdded].nTr;
/* Need to re-filter and re-sort the table to get positioning correct, not perfect
* as this will actually redraw the table on screen, but the update should be so fast (and
* possibly not alter what is already on display) that the user will not notice
*/
this.oApi._fnReDraw(oSettings);
/* Find it's position in the table */
var iPos = iAdded;
/* Get starting point, taking account of paging */
if (iPos >= 0) {
oSettings._iDisplayStart = (Math.floor(iPos / oSettings._iDisplayLength)) * oSettings._iDisplayLength;
this.oApi._fnCalculateEnd(oSettings);
}
this.oApi._fnDraw(oSettings);
return {
"nTr": nAdded,
"iPos": iAdded
};
};