Deleting Selected Rows Not Quite Working

Deleting Selected Rows Not Quite Working

dredgydredgy Posts: 16Questions: 5Answers: 0

This is probably a problem with my logic rather than datatables specifically, but I can't spot it and that's probably because I'm not familiar with jQuery and Datatables syntax.

I have a table, where you should be able to select rows and delete them. I'm 99% sure I had this working completely and then seem to have screwed something up!

Here is a picture of the rows I want to delete:
Here is a picture of the rows that actually get deleted:

The voidThings() function is assigned to a button, and depending on if there are rows selected or not, it will do different things.

//Either void selected things, or else toggle void
//mode on, so that the next item selected is deleted
function voidThings(){
    var selectedRows = orderBox.rows('.selected').count();
    if(selectedRows > 0){
        voidSelectedItems();
    } else {
      if(voidMode){
        setVoidMode(false);
      } else {
        setVoidMode(true);
      }
    }
    
    updateTotal();
}

//Updates the total price.
function updateTotal(){ 
     newTotals = "$"+orderBox.column(3).data().sum().toFixed(2);
     
     $('.orderBoxTotals').text(newTotals);
     updateSelectedTotal();
     
}

//Updates selected totals
function updateSelectedTotal(){
    selectedRows = orderBox.rows('.selected');
    
    newTotals = "$"+orderBox.rows( {selected: true} ).data().pluck(3).sum().toFixed(2)+" Selected";
    
    $('.selectedTotal').text(newTotals);
}

In this instance, it should go straight to voidSelectedItems(). In this case orderQuantity is == 1.

//Remove selected items from the order box.
function voidSelectedItems(){
    selectedRows = orderBox.rows('.selected');
    voidQty = globalQty;
    
    
    //Loop through each selection
    selectedRows.every(function (rowIndex, tableLoop, rowLoop ) {
        activeRow = orderBox.rows(rowIndex);
        orderQty = getOrderQty(activeRow);
        
        if(orderQty > 1){
            updateOrderQty(activeRow, 'subtract', voidQty);
        } else {
            selectedRows.deselect();
            activeRow.remove().draw();
        }
    });    
}

//Get the qty of a selected row.
function getOrderQty(row){
    result = orderBox.cell(row,1).data();
    
    //Result is sometimes returning NaN in seemingly random circumstances.
    //This is not meant to be a permanent solution.
    if(isNaN(result)){
        result = 1;
    }
    console.log(result);
    return result;
}


And the datatables init:

$(document).ready( function () {
            orderBox = $('#orderBox').DataTable( {
                select: {
                    style: 'multi'
                },
                autoWidth: false,
                paging: false,
                columns: [
                    { "visible": false}, { "width": "50px" },{ "width": "240px;" },{ "width": "70px" },{ "width": "70px" }, {"visible":false}
                ],
                ordering: false,
                searching: false,
                language: {
                    info: "",
                    infoEmpty: "",
                    emptyTable:"",
                    zeroRecords:"",
                    select: {
                        rows: ""
                    }
                 },
            } );
            

Gonna keep plugging away at it, but it's doing my head in so if you can spot a flaw in my logic, then please let me know!

Answers

  • dredgydredgy Posts: 16Questions: 5Answers: 0
    edited November 2020

    So it seems to be in these two lines:

    selectedRows.deselect();
    activeRow.remove().draw();
    

    Occurring inside the loop, having previously been outside of a loop. So instead of deselecting and removing the rows, I assign a class, "toBeDeleted" and then delete all rows of that class outside of the loop.

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    hi
    if selected row count is 1, why do you deselect it before deleting?

    https://editor.datatables.net/reference/api/remove()

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Yep, as Lapointe said, you should just be able to say selectedRows.remove().draw(),

    Colin

This discussion has been closed.