...
Note that CHEFS uses the form.io drag and drop form builder that has components such as ‘Text Field’ and ‘Number’ components which are included within both ‘Basic’ and ‘Advanced’ fields. The basic components offer limited functionality so ensure that you are using the advanced options for the calculations documented below.
...
Basic Arithmetic
Using the ‘Number’ component from the ‘Advanced Fields,’ simple calculations can be carried out based on user input. Each component in the form uses a unique key which can be edited by clicking on the settings (gear) icon > API > Property Name.
...
A component is accessed in another field using the data
...
unique keys
...
keyword and the components calculated value is set using the value
variable under Settings > Data > Calculated Value.
...
For example, if you wanted to add the input from two fields whose unique keys were num1
and num2
, this can be achieved using the following code:
Code Block |
---|
value = data.num1 + data.num2 |
Similarly, basic arithmetic operations can be carried out to subtract, multiply, or divide:
Code Block |
---|
value = data.num1 - data.num2
value = data.num1 * data.num2
value = data.num1 / data.num2 |
...
Note: If you are calculating a value based on the input from more than two fields, cast the data variables to a Number()
so that the default value is set to 0
, otherwise the calculated field will concatenate the values, for example, the result of 1 + 1
will be displayed at 11
.
Code Block |
---|
value = Number(data.num1) + Number(data.num2) + Number(data.num3) |
...
Date/Time Calculations
CHEFS can also be used for date/time calculations using the Moment.js library. Moment is a widely popular Javascript library designed to make date/time manipulations easier. In the following example, it is used to calculate the number of days between two dates.
...