Calculated Fields
Calculated fields automatically compute values based on other form inputs.
Basic Calculations
To create a simple calculated field:
- Add a Number component to your form
- Go to the API tab (Technical Properties)
- In the Calculated Value field, enter a JavaScript formula
- Reference other fields using
data.fieldKey
Example: Calculate total by multiplying quantity and price:
value = data.quantity * data.price;
Formatting Calculated Results
You can format the results of calculations:
- Use JavaScript methods to format the result
- Set the appropriate number of decimal places
- Add currency symbols or other formatting
Example: Format a currency calculation:
value = (data.quantity * data.price).toFixed(2);
Calculations with Data Grids
For calculations involving data from data grids:
- Reference data grid values using
data.gridName[rowIndex].fieldName - Use array methods to process multiple rows
- Consider using reduce() for summing values
Example: Calculate sum of amounts in a data grid:
value = data.lineItems.reduce(function(sum, row) {
return sum + (parseFloat(row.amount) || 0);
}, 0);
Date Calculations
To calculate with dates:
- Add a calculated field (Number or Text Field)
- In the Calculated Value field, use JavaScript date functions
- Convert form date strings to Date objects for calculations
Example: Calculate days between two dates:
// Calculate days between startDate and endDate
const start = new Date(data.startDate);
const end = new Date(data.endDate);
const diffTime = Math.abs(end - start);
value = Math.ceil(diffTime / (1000 * 60 * 60 * 24));