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?
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.
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.
Answers
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
I am using 1.4 beta. Ideally I would like to show the calculation in the modal window while the user types.
Okay, so in 1.4.0-beta.1 which I've just posted you could do something like this:
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 (thevalues
property of the data object given contains the current values of the form) and then usingfield().message()
to write a calculation into a field's message box.Regards,
Allan