fnDeleteRow matching rules

fnDeleteRow matching rules

barrykeepencebarrykeepence Posts: 22Questions: 0Answers: 0
edited December 2012 in General
I made a mistake in some code and it gave some strange behaviour.

This is the code:
[code]
var selectedrows = document.querySelectorAll('.row_selected');
console.log (selectedrows);
console.log ( selectedrows.length + " rows to delete");
// cycle through
for ( var i = 0; i< selectedrows.length; i++) {
// delete it
oTable.fnDeleteRow( selectedrows[0], null, false );
}
oTable.fnDraw();
[/code]

The mistake was that I had [0] in the loop and not [i]. I would expect it to delete the first row and then fail.
it didn't! I deletes multiple rows and not the ones required!
Why?
How does fnDeleteRow use a tr element to match to internal data structures?

Replies

  • barrykeepencebarrykeepence Posts: 22Questions: 0Answers: 0
    Looking at the code it seems that the function looks up the underlying row index from the TR itself using ._DT_RowIndex.
    If you log selected rows[i]._DT_RowIndex you can see this.

    It seems that when the first row is deleted, the indexes are recalculated and the TRs changed. Essentially, when you pass a TR object in it is pass by reference (and datatables changes the _DT_RowIndex. This also explains why passing in the TR works when deleting multiple rows and passing integers does not.

    Essentially passing integers is pass by value and passing TRs is pass by reference.

    It has always bothered me _why_ passing the TRs works and passing integers does not.

    Allan - is this right?
This discussion has been closed.