Automatic change of one field after changing another

Automatic change of one field after changing another

kekerkeker Posts: 14Questions: 4Answers: 1

Hello. Can I make it so that when the value in one of the cells changes, the function of updating the value in another cell automatically works? For example, there are fields "Number 1", "Number 2" and "Sum". After changing the fields "Digit 1" or "Digit 2" the function of the amount recalculation should work and further the received value should be updated in the "Amount" cell. All this should be preserved in the database, and then return the value to the cell.

Answers

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

    Hi @keker ,

    Take a look at this example - it's called dependent fields. You define them with dependent().

    Cheers,

    Colin

  • kekerkeker Posts: 14Questions: 4Answers: 1

    But how to apply dependent() under several fields at once?

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

    If you look at the reference page in my second link above, it says that field "can also be given as an array of field names to allow the same callback options to be used for multiple fields.".

  • kekerkeker Posts: 14Questions: 4Answers: 1

    I did it. But the method dependent() is called on the entire line, even when the focus is on another cell not specified in the array of fields.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Hi,

    You would need to use dependent() on the two fields together. For example you could do:

    editor.dependent( [ 'number1', 'number2' ], function ( val, data, callback ) {
      return {
        values: {
          sum: data.values.number1 * data.values.number2;
        }
      };
    } );
    

    Allan

  • kekerkeker Posts: 14Questions: 4Answers: 1

    And the last question. The docs says: "You can also modify the event listener using the event option of the third parameter". But in your example third example is "callback". How can I get the type of event?

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

    Hi @keker,

    That's the final example on that page for dependent(). That third option can be one of several things (event, data, preUpdate and postUpdate), they're described higher up on that page.

    Cheers,

    Colin

This discussion has been closed.