Perform dynamic calculation in editor modal

Perform dynamic calculation in editor modal

tduneventtdunevent Posts: 4Questions: 2Answers: 0

Is it possible to perform a calculation within the editor modal? For example, if I have a cost field and a units field, is it possible to calculate and display the cost per unit while the user enters the data into the modal?

This question has an accepted answers - jump to answer

Answers

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

    Sure - where do you want to show the calculation result? Are you using 1.4 beta or the 1.3 release? I'm just about to release a new version of the beta which adds a couple of nice little options to make this easier, but in 1.3 you would need to use field().node() to get the node, the $('input', editor.field('myField').node()).on( 'keyup', ... ); etc - i.e. bind an event handler to the key press where the event handler will do the calculation and display of the result.

    Allan

  • tduneventtdunevent Posts: 4Questions: 2Answers: 0

    I am using 1.4 beta. Ideally I would like to show the calculation in the modal window while the user types.

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Okay, so in 1.4.0-beta.1 which I've just posted you could do something like this:

    editor.dependent( 'myField1', function ( val, data ) {
      editor.field( 'myCalcedField' ).message( data.values.myField1 * data.values.myField2 );
    } );
    

    This uses the dependent() method (which I've just spotted has a formatted error on it I will address shortly).

    Basically the callback function is triggered whenever myField1's value changes. So your callback can do more or less whatever it wants. In this case I've got it using two values from the form (the values property of the data object given contains the current values of the form) and then using field().message() to write a calculation into a field's message box.

    Regards,
    Allan

This discussion has been closed.