我正在使用我的位置:
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
};
};