fnGetPosition returns -1 for iColumnIndex when adding a new row and then clicking on the row 0

fnGetPosition returns -1 for iColumnIndex when adding a new row and then clicking on the row 0

xaisoftxaisoft Posts: 7Questions: 0Answers: 0
edited August 2013 in General
I am getting my position using:

[code]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;
};[/code]

If I click into row 0, iDataIndex contains 0 and iColumnIndex contains whatever column I clicked in. But if I add a new row, lets say row 9 and then I click into row 0, iDataIndex still holds 9 instead of 0 and iColumnIndex returns -1.

fnNodeToDataIndex looks like this:

[code]
function _fnNodeToDataIndex( oSettings, n )
{
return (n._DT_RowIndex!==undefined) ? n._DT_RowIndex : null;
}
[/code]

fnNodeToColumnIndex looks like this:

[code]
function _fnNodeToColumnIndex( oSettings, iRow, n )
{
var anCells = _fnGetTdNodes( oSettings, iRow );

for ( var i=0, iLen=oSettings.aoColumns.length ; i

Replies

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    Could you show me what your external code is please? I don't really see why clicking on the same row twice would change depending on if you add a new row or not. A live example would be extremely useful!

    Thanks,
    Allan
  • xaisoftxaisoft Posts: 7Questions: 0Answers: 0
    Allan,

    Here is code that I run when I add a new row. I turn bServerSide off and then turn it back on, otherwise, clicking the Add New Row button will make an ajax call:

    [code]
    addBlankRow = function(el, e, oSettings) {

    var self = this,
    $button = $(el),
    $table = $(oSettings.nTable).dataTable(),
    nTr = $table.$('tr:first').get(0),
    data = $table.fnGetData(nTr),
    aoColumns = oSettings.aoColumns;

    $.each(data, function(i, item) {
    var aoCol = $.grep(aoColumns, function(n, index) {
    return n.mData == i;
    });

    if (aoCol.length) {
    if (aoCol[0].options.type === "AutoIncrement") {
    data[i] = 0;
    } else if (!aoCol[0].options.refOnly) {
    if (typeof item === 'number') {
    data[i] = null;
    } else if (typeof item === 'string') {
    data[i] = '';
    }
    }
    }
    });

    $table.fnPageChange('last', false);
    oSettings.oFeatures.bServerSide = false;
    var returnObject = $table.fnAddDataAndDisplay(data, false),
    $row = null;
    if (returnObject.nTr == null || returnObject.nTr == undefined) {
    $row = $table.$('tr:last')
    .addClass('newRow ')
    .show('slow')
    .find('td:not(:first-child)');
    } else {
    $row = $(returnObject.nTr)
    .addClass('newRow ')
    .show('slow')
    .find('td:not(:first-child)');
    }
    $.scrollTo($row);
    oSettings.oFeatures.bServerSide = true;
    }
    [/code]
  • xaisoftxaisoft Posts: 7Questions: 0Answers: 0
    Allan, I also noticed when I call addBlankRow and then update a field in row 0, it clears out all the fields in row 0. This only happens to row 0, not any other row and it only happens when I add a new row first. I am using fnUpdate to update the row cell. Here is the function:

    [code]

    "fnUpdate": function(oSettings, fnDraw) {
    var iListLength = 5;
    var oPaging = oSettings.oInstance.fnPagingInfo();
    var an = oSettings.aanFeatures.p;
    var i, j, sClass, iStart, iEnd, iHalf = Math.floor(iListLength / 2);

    if (oPaging.iTotalPages < iListLength) {
    iStart = 1;
    iEnd = oPaging.iTotalPages;
    }
    else if (oPaging.iPage <= iHalf) {
    iStart = 1;
    iEnd = iListLength;
    } else if (oPaging.iPage >= (oPaging.iTotalPages - iHalf)) {
    iStart = oPaging.iTotalPages - iListLength + 1;
    iEnd = oPaging.iTotalPages;
    } else {
    iStart = oPaging.iPage - iHalf + 1;
    iEnd = iStart + iListLength - 1;
    }

    for (i = 0, iLen = an.length; i < iLen; i++) {
    // Remove the middle elements
    $('li:gt(1)', an[i]).filter(':not(.next)').remove();

    // Add the new list items and their event handlers
    for (j = iStart; j <= iEnd; j++) {
    sClass = (j == oPaging.iPage + 1) ? 'class="active"' : '';
    $('' + j + '')
    .insertBefore($('li.next:first', an[i])[0])
    .bind('click', function(e) {
    e.preventDefault();
    oSettings._iDisplayStart = (parseInt($('a', this).text(), 10) - 1) * oPaging.iLength;
    fnDraw(oSettings);
    });
    }

    // Add / remove disabled classes from the static elements
    if (oPaging.iPage === 0) {
    $('li.prev', an[i]).addClass('disabled');
    } else {
    $('li.prev', an[i]).removeClass('disabled');
    }

    if (oPaging.iPage === oPaging.iTotalPages - 1 || oPaging.iTotalPages === 0) {
    $('li.next', an[i]).addClass('disabled');
    } else {
    $('li.next', an[i]).removeClass('disabled');
    }
    }
    }
    [/code]
  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    > I turn bServerSide off and then turn it back on

    Yikes! That's completely not supported and I'm not surprised that things are breaking if you are dynamically changing the values in the settings object.

    If you are using server-side processing, then the client-side fnAddData function is useless since the new data won't take into account filtering, sorting etc. If you are using server-side processing you need to add the data at the source (i.e. the server) and then call fnDraw to get the latest test.

    Allan
  • xaisoftxaisoft Posts: 7Questions: 0Answers: 0
    edited August 2013
    Allan,

    Surprisingly, there aren't too many issues. Typically fnAddData is used just to add a row, but a user must save the row to the database first before they can sort/filter/etc on it. I think a potential problem is that I am calling fnGetPosition to many times which is returning incorrect results.

    Also if is there a better way to add a row because what is happening is that I have a button that adds a blank row to the end of the table if I have bServerSide = true, it makes an ajax call to get the data and it never adds the row, the only way I have been able to get around it is to set bServerSide = false and then call fnAddData. How can I add a row to the data source if certain field, (primary keys) are necessary)?
  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    While there might not be any obvious issues, as I say, its not a supported interaction (changing any settings object value interactively isn't supported - that's what the public API is there for) so I'm not sure what will be going wrong at the moment.

    The whole point of server-side processing is that the processing is done at the server and every draw needs an Ajax cal. That includes updating the table for new data. So you would instruct the server to add the data via one ajax call and then reload the data with another.

    I guess an obvious question is - do you really need server-side processing? Are you working with 50'000+ rows?

    Allan
  • xaisoftxaisoft Posts: 7Questions: 0Answers: 0
    edited August 2013
    Allan,

    Not as many as 50,000 (but it might go as high as that or higher), but may columns, there are 150+ columns for 1 table. I had a another question:

    I am doing the following to get the first row:

    [code]
    var nRow = cell.$table.fnGetNodes(cell.aPos[0]);

    cell.$row = $(nRow);
    cell.currentRowData = cell.$table.fnGetData(nRow);

    [/code]

    But when I add a new row with fnAddData and call this same code with nRow as 0, currentRowData holds the data for the added row, not the first row in the table. cell.aPos[0] holds the row index position.
  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    If this is with the change in the server-side processing mode, then all bets are off, and I'm not surprised that it doesn't work. As I said, that is not supported. Sorry.

    Allan
This discussion has been closed.