Upgraded to DataTables 1.7.2 - fnDeleteRow still throws error when first row is deleted

Upgraded to DataTables 1.7.2 - fnDeleteRow still throws error when first row is deleted

PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
edited September 2010 in General
The error is thrown only when the first row is selected, but it doesn't happen all the time. here is the error:

Line: 1754
Error: 'aoData[...].nTr' is null or not an object

Here is the function calling fnDeleteRow:

[code]
function deleteIngredients() {
var numRows = ingredientsTable.fnGetData().length;
var isAtLeastOneRowSelected = false;

for (var i=0; i < numRows; i++) {
var node = ingredientsTable.fnGetNodes(i);

if ($(node).hasClass('row_selected')) {
isAtLeastOneRowSelected = true;
var currentData = ingredientsTable.fnGetData(i);
if (confirm("Are you sure you want to delete Ingredient " + currentData[1] + "?"))
{
ingredientsTable.fnDeleteRow(node,null,true);
}
}

}

if (!isAtLeastOneRowSelected)
{
alert("No ingredient was selected.");
}
}
[/code]

* Edited by Allan to add code highlighting (make life easier :-) )

Replies

  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    Actually, I got the error by deleting the 2nd row as well as the first. Something about this line:

    return oSettings.aoData[iRow].nTr;

    In this particular instance, aoData has 3 elements, and iRow=3, which is a problem because aoData[2] is the third element and thus there is no aoData[3].

    I'm willing to hack a fix for this at this point, if someone can give me some guidance on a way to do it without breaking Datatables. We really need to get this to work asap.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It looks like you are passing in '3' to fnGetNodes then for some reason, which of course it doesn't like since there isn't 4 rows, as you point out. I think the basic premise of this code is slightly dodgy since you have an independent array working on an array which is changing dynamically as the table updates. I'd suggest getting a list of the selected rows by doing $('tr.row_selected', ingredientsTable.fnGetNodes()), and then looping over that, deleting each one in turn.

    Allan
  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    I got this error message:

    Line: 1734
    Error: 'aoData[...]._aData' is null or not an object

    Here is the code:

    $('#ingredientsTable tr.row_selected').each(function(index) {
    isAtLeastOneRowSelected = true;
    var currentData = ingredientsTable.fnGetData($(this));
    if (confirm("Are you sure you want to delete Ingredient " + currentData[1] + "?"))
    {
    ingredientsTable.fnDeleteRow($(this),null,true);
    }
    });

    I'll keep trying...
  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    Ok, this seems to work - no more Javascript errors:

    $('#ingredientsTable tr.row_selected').each(function(index) {
    isAtLeastOneRowSelected = true;
    alert('line ' + index);
    var currentData = ingredientsTable.fnGetData(index);
    if (confirm("Are you sure you want to delete Ingredient " + currentData[1] + "?"))
    {
    ingredientsTable.fnDeleteRow(index,null,true);
    }
    });

    Changed $(this) to index
This discussion has been closed.