bulk edit without user interaction

bulk edit without user interaction

arcanumarcanum Posts: 15Questions: 4Answers: 0

Hello, here I am again :smile:

How do I trigger multiple row updates, after a user has changed a row?

I have some rows which are linked by a column, let's call it sub_id.
if a user updates a field name with the bubble or form editor (multiple fields), I want editor to update that field (fields) in all rows with the same value in sub_id without the user having to do anything.

how would i achieve that? I am using php server side, if that matters

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited November 2022

    I am using php server side, if that matters

    I assume you are also using the Editor PHP libraries.

    If so, just use SQL and Editor's db handler on "validatedEdit" (meaning: the editing can't go wrong any longer.) You could also use "postEdit", I guess. This will work provided you are submitting all values to the server in case you are editing a row. This is the default for "main" but not for "bubble" editing.
    https://editor.datatables.net/reference/option/formOptions

    ...
    ->on( 'validatedEdit', function ( $editor, $id, $values ) use ( $db ) {    
        $db->raw()
           ->bind( ':name', $values["name"] )
           ->bind( ':sub_id', $values["sub_id"] )    
           ->bind( ':id', $id )
           ->exec( 'UPDATE yourTable
                       SET name     = :name
                     WHERE sub_id   = :sub_id
                       AND id      <> :id' );
    })
    
  • arcanumarcanum Posts: 15Questions: 4Answers: 0

    Hi rf1234,

    thank you for that idea, but I am looking for a client-side solution.

    doing it server-side bypasses all the formatters and I have to trigger ajax.reload() on the whole table after each edit, to get the rows updated.
    Also I cannot use the editor db handler, because I don't know in advance, which and how many fields are updated.

    It's working with a 'validateEdit' function, but it's messy and I hope, there is a 'cleaner' way of updating the corresponding rows :smile:

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    The cleanest way would be to adjust your data model I suppose. "name" shouldn't be saved redundantly but just once. That would be a solution not requiring a client side update. A simple ajax.reload after updating "name" once would do the job.

    If you do client side manipulations without updating / reloading the values from the server this may become very "messy", I guess.

  • arcanumarcanum Posts: 15Questions: 4Answers: 0

    You are right, the data should not be shared across several rows, but I need that row to be displayed up to 5 times (depending on start and end date) with only the start date being different

    If you do client side manipulations without updating / reloading the values from the server this may become very "messy", I guess.

    I do not want to manipulate the data client side.
    If a field on a row gets updated, what I want to do client side is looking for rows with the same sub_id and then an editor.edit() should be triggered with the values from the edited row for every row with the same sub_id.

    Is that possible?

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited November 2022 Answer ✓

    Something like this could work but it isn't "simple" but rather "messy" I guess.
    It requires global variables to check what has changed. You also need to distinguish whether a successful submission was "manual" or "automatic". It has to be sure that the "opened" event will not be triggered when submitting automatically without showing the form etc.

    //global variables
    var previousName = "";
    var manualEditing = false;
    
    editor
        .on( 'opened', function ( e, type, action ) {
            previousName = "";
            if ( action === "remove" ) {
                return;
            }
            if ( action === "edit" ) {
                previousName = this.val('name');
            }
            manualEditing = true;
        })    
        .on( 'submitSuccess', function ( e, json, data, action ) {
            //we only do something if the editing was manual!
            if ( action === "remove" || typeof json.data[0] === 'undefined' ||
                 ( ! manualEditing ) ) {
                return;
            }
            //we need to reset the flag because the next successful submissions are
            //likely to be automatic WITHOUT opening a form. Those automatic 
            //submissions shouldn't trigger more updates (loop!).
            manualEditing = false;
            var nw = json.data[0];        
            
            table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                var data = this.data();
                if ( data.sub_id === nw.sub_id && data.name === previousName ) {
                    editor.edit( this, false )
                          .set( { 'name': nw.name } )
                          .submit();       
                }
            });
        });
    
  • arcanumarcanum Posts: 15Questions: 4Answers: 0

    Hi rf1234,

    sorry for my late response. I was not able to work on my project for a while.
    Thank you very much for your solution! It works like a charm and is (with some minor adjustments) exactly, what I was looking for.

Sign In or Register to comment.