calculate fields in Create/Update screen

calculate fields in Create/Update screen

agustin_garciaromeroagustin_garciaromero Posts: 42Questions: 0Answers: 0
edited November 2012 in Editor
Hello,
I'm looking for a way to populate a readonly third field based on the changes on any of field one or two when user is either Creating or Updating a record.
The third field wil include some switch/case like rationale to determine its value.
Depending on whats easier or more secure, it can be done at the client or server side.
NOTE: There is no actual requirement for this field to be displayed to the user, though it could be more user friendly.
Thanks in advance

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    If you don't need to show it to the user, you could use `onPreCreate` / `onPreEdit` to manipulate the data that is being sent to the server and add your calculated value there.

    However, to show the value to the user, what you'd need to do is bind an event to the input nodes in the form. You could use the new `node` method to do something like this:

    [code]
    $( 'input', editor.node( 'my_field' ) ).on( 'change keyup', function () {
    editor.val( 'output_field', 'my-calculated-value' );
    } );
    [/code]

    So what is happening there is that we are finding the 'input' element of the field with the name "my_field", assigning change and keyup events to it (you might want one, both or something else), and then when that event fires, assigning a value to a different Editor field which will be immediately updated for the user to see, and that value also submitted to the server.

    Regards,
    Allan
  • agustin_garciaromeroagustin_garciaromero Posts: 42Questions: 0Answers: 0
    Hello,
    I didn't understand what the 'input' part of the snippet is for, but I made it work by inserting the following code:
    $(editor.node( 'parent' )).on( 'change keyup', function () {
    editor.set( 'child', 'TEST' );
    } );

    That works when 'child' is a TEXT field, but if I make it a SELECT, it doesn't respond to it.
    Not sure:
    a)If the 'input' part of your code has anything to do
    b)If I need to dynamically change the nature (from TEXT to SELECT -and even if I could-) of the html objeect
    c)If I need to use two 'child' fields and hide/show one or the other depending on user selection

    In my case, the selection of Option A, may lead to a text entry option, while Option B may lead to a sub-select entry, C could be a select as well, etc.
  • agustin_garciaromeroagustin_garciaromero Posts: 42Questions: 0Answers: 0
    Hello,
    Any comment on this one? Have someone done/required something like this?
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    edited November 2012
    My example is selecting `input` elements inside the field `my_field` . The event will fire on change or keyup. Since a select menu isn't an `input` element, but rather a `select` on the selector of course won't match anything and thus would never fire. You need to modify the selector as appropriate.

    Allan
This discussion has been closed.