fnDeleteRow matching rules
fnDeleteRow matching rules
barrykeepence
Posts: 22Questions: 0Answers: 0
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?
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?
This discussion has been closed.
Replies
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?