Delete row in database after form submit

Delete row in database after form submit

schwaluckschwaluck Posts: 103Questions: 27Answers: 1
edited September 2020 in Free community support

Hey there,

I have the following issue. I have a table with the select type "multi". Now I want to delete the selected rows after they got updated. Because they get copied in another table based on a mysql "before update" trigger and I do not need those rows in the current table anymore.
I thought about an editor event to solve this problem but I struggled with catching the rows I want to delete on "submitSuccess".
This is my current code for solving this issue:

editor
      .title( "Transfer" )
      .field( "name" )
      .buttons( {
        label: "transfer",
                fn: function () {
                   var row = table.row( $(this).parents('tr') );
                       this.submit();
                }
       } )
       .edit();
editor.on( 'submitSuccess', function () {
     row.delete();
} );

Can any of you help me with this, or is there a better way to solve my problem? :)

Greetings
Daniel

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    You can get all the selected rows by calling rows() with selector-modifier set to {selected: true}. That could then be passed into remove() to remove them from the table both locally and on the server,

    Colin

  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1
    edited September 2020

    Hey Colin,
    thanks for your quick response!

    Unfortunately, I can't get it integrated properly or I have misunderstood something.
    My current code now looks like this:

    editor
          .title( "Transfer" )
          .field( "name" )
          .buttons( {
            label: "transfer",
                    fn: function () {
                        var row = table.rows( { selected: true } );
                        this.submit();
                    }
           } )
           .edit();
    
    
        editor.on( 'submitSuccess', function () {
            editor.remove(row);
        } );
    
    

    Would you mind to tell me what I made wrong? :)

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin

    |Are they already deleted on the server-side? Or do you need to send a delete command to the server?

    The way I'd approach this myself, is to use a postEdit event handler on the server to delete the row - Editor should then take care of removing the row from the client-side for you.

    Allan

  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1

    Hey Allan,
    thanks for your response!

    Right, I need to send a delete command on update of the selected rows to the server. Now I took a look on your suggeseted approach and got the following code in my php file. However it does not work. Would you mind to tell me if I made something wrong here? :)

    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
            $row.remove();
    } )
    
  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin
    Answer ✓

    $row there is a PHP array of data. It looks like you are treating it like a Javascript class instance.

    What you'd need to do is make use of the database connection in PHP. This part of the event handler page shows how it can be used, and this page has the full reference docs.

    In this case, you might use something like:

    $editor->db()->delete('myTableName', [ 'id' => $id ]);
    

    Allan

  • schwaluckschwaluck Posts: 103Questions: 27Answers: 1

    Hi Allan,

    thank you so much!

    This solved my issue.

    Kind regards,

This discussion has been closed.