How do I use preEdit when editing multiple rows at the same time

How do I use preEdit when editing multiple rows at the same time

markco84markco84 Posts: 13Questions: 1Answers: 0

I think I am having the same problem as this but cannot figure it out from the answer given.
https://datatables.net/forums/discussion/51227/preedit-and-multiple-editing

->on( 'preEdit', function ($editor, $id, $values ) {
      $editor
          ->field( 'ordersvalves.searchstring' )
          ->setValue($values['ordersmaster']['type'] . $values['ordersmaster']['model'] . $values['ordersvalves']['size'] . $values['ordersvalves']['chainwheel']);
    } )

If I am editing multiple rows at the same time I get the same result for all rows.
Can anyone clarify?
Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Hi,

    preEdit will be triggered once for each row, but it is triggered for all rows before actually saving any of them. So the value saved here would be the one form the last row that is edited.

    What you can do is this:

    ->on( 'preEdit', function ($editor, $id, &$values ) {
          $values['ordersvalves']['searchstring'] = '...';
    } )
    

    Note the & which denotes the "pass by reference" that was referred to in the thread you linked to. That means that you can modify the value and that will be reflected in the variable the caller sees as well.

    Allan

  • markco84markco84 Posts: 13Questions: 1Answers: 0

    Thanks Allan,

    I tried using a preSubmit on the client side and it seems to work as expected. Is one way better than the other or will both work the same?

    editor.on( 'preSubmit', function ( e, d, action ) {
        if ( action === 'edit' ) {
        $.each( d.data, function ( key, row ) {
          row.ordersvalves.searchstring = row.ordersmaster.type+row.ordersmaster.model+row.ordersvalves.size+row.ordersvalves.chainwheel;
        } );
        }
        });
    

    Thanks,

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

    Both work the same. The benefit of server-side is that you're preventing any attacks that could send data direct outside of the browser. If you're in a safe intranet, that shouldn't be an issue.

    Colin

This discussion has been closed.