Silently removing row not working - remove(row, false);

Silently removing row not working - remove(row, false);

arcanumarcanum Posts: 15Questions: 4Answers: 0

Link to test case: https://sahr.it (user: test // password: abcd1234)
Debugger code (debug.datatables.net): onelas
Error messages shown: none
Description of problem:

Hi, I am trying to delete some rows in a loop without user interaction. It is working, if I do not bypass the form and click delete for every interation.
editor.remove( this.index() ).submit();

As soon as I add the a false parameter to remove, editor is not removing the rows. I think it's the remove functions, because the submit events are not triggered as far as I can see
editor.remove(this.index(), false).submit();

The hole loop:

terminTabelle.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
    var data = this.data();
    if ( data.ferien_id === nw.ferien_id ) {
         if ( action === "edit" ) {
            editor.edit(this, false)
                .set(nw)
                .submit();
        }
        if ( action === "remove" ) {
            console.log(action);
            console.log(this.index());
            editor.remove(this.index(), false);
            editor.submit();
         }
    }
}); 

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Answer ✓

    The issue here is that you can have multiple actions all happening at the same time here. Editor can only have a single action active at a time, but its form submission is async.

    So consider two rows to be deleted - you loop over them resulting in:

    1. Send delete for row 1
    2. Send delete for row 2
    3. Receive response from row 1
    4. Receive response from row 2.

    At some point in future, Editor will support that (probably in v3, whenever that happens :)), but at the moment it does a single action only. So you need to wait for row 1's deletion acknowledgement first.

    You can do that by providing a callback to the submit() function, and then move on to the next row (i.e. make a queue). However, I think you'd be much better off in this case to just send a single Ajax request to delete all the rows you want to delete, and then move on to submit the request for the edit (in the submit() callback).

    So use your loop to get the id of all rows you want to delete, and trigger remove() with that array rather than doing it one at a time. It will be a heck of a lot faster as well!

    Allan

  • arcanumarcanum Posts: 15Questions: 4Answers: 0

    Again, thank you very much Allen!

    just in case someone else has a similar issue, I solved this by

    if ( action === "remove" ) {
        deleteRows.push(this.index());
    }
    

    in the loop and

    .on( 'submitComplete', function( e, json, data, action ) {
        if (deleteRows.length > 0 && action === 'remove') {
            editor.remove(deleteRows, false).submit();
            deleteRows = [];
        }
    });
    

    Patrick

Sign In or Register to comment.