0

I want to remove some rows from datatable based on their respective indexes.The indexes are in form of an array. Upon clicking a button the function gets the array and finds each of them in the datatable then remove them .I have been able to create the code and its able to execute perfectly.Now the issue is not all the rows are removed,lets say we have 5 rows that we want to remove only 2 rows are removed.How can i remove all the rows in the table with all the respective indexes.This is my code

var productsIds=['2','3','4','5','6']
var productsTable=$('#productsTable').DataTable()

productsIds.forEach(function(id) {
    var columnIndex = 0;
    var rowIndex = productsTable.column(columnIndex).data().indexOf(id);
    productsTable.row(rowIndex).remove();
});

productsTable.draw();
5
  • Now the issue is that not all the rows are removed: on the data table or the database side? Commented Jun 14 at 7:24
  • Can you please provide a minimal-reproducible-example with the needed html aswell. preferably as a code snippet. Commented Jun 14 at 7:29
  • on the server side everything is okey.on the client side its where only a few rows are removed Commented Jun 14 at 7:30
  • The documentation for the row.remove method says, "Please be aware that this method removes the data from the table internally but that action won't be visually shown until the draw() method is called to update the display. " - now that doesn't exactly clarify what "internally" means, but my guess here would be, that productsTable.column(columnIndex).data() still operates on the original data set, even after rows were removed - so that the index you determine based on that, will simply be "off". [...]
    – CBroe
    Commented Jun 14 at 7:54
  • [...] If that is indeed the case - then loop over your product IDs to determine all the row indexes first, and store them into an array - without removing rows at that point already. And then loop over that array, to actually remove those rows. Also, not sure if removing a row will update the index of all following rows internally - so to be save, I would sort the row IDs in descending order first, and then remove them in that order.
    – CBroe
    Commented Jun 14 at 7:54

1 Answer 1

1

Remove rows in reverse order to avoid index-shifting issues or Use a different approach that avoids modifying the table while iterating through it Reverse the productsIds array This ensures that when rows are removed the indexes of the rows that are yet to be removed are not affected Check if rowIndex is valid Before attempting to remove a row make sure that rowIndex is not -1, which indicates that the row with the given ID was not found Alternatively, you can collect the rows to be removed first and then remove them in a separate loop:

Collect indexes first Store the indexes of the rows to be removed in an array. Sort and remove Sort the array in descending order to remove rows from the end of the table first, thus avoiding index-shifting issues Remove rows Iterate through the sorted array to remove the rows Using either approach will ensure that all specified rows are correctly removed from the DataTable

1
  • 1
    thanks alot it worked for me..i added the indexes to an an array then deleted each row for each index Commented Jun 14 at 8:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.