Totals
Is it possible to have an element (tp_tot) in a form that gets the results of the previous 4 Select elements (tp1, tp2, tp3 & tp4) and adds them together to get a score total? It wont be visible to the user but will be present in the table view.
tp1 to tp4 are all "Select Elements", they all use the same option list (tp_score) with four options to choose from (1, 2, 3 or 4).
If this is possible, could someone explain how its done, i have searched around the site and found several likely possibilities but i just cannot seem to get them to work?
Thank you in advance
Steve
-
Official comment
Hi Steve,
Good question and we can definitely help you out here!
The first thing to know is that option-based elements have three different values which can be used in calculations:
- Sort Order (also called an index) is the position within the option list. It can be useful for simple option lists but in our experiences is less flexible over time because updates will often alter the sort order of a given option. To reference the sort order, just use the element's name.
- Label is the text that you see on the form itself. The label can be localized which makes comparisons tricky so it's not a primary choice for these types of challenges. To reference the label, use ZCDisplayValue_name where `name` is the element's name.
- Key value is the stored value (what we actually write to the database) and must be unique within an option list. For these reasons we generally recommend using the key value for any smart controls. To reference the key value, use ZCDisplayKey_name where `name` is the element's name.
With all that being said, your specific use case would make sense for sort order or key value. I'll go through both and talk about the pros/cons.
When making the element references you want to use the form path which means the reference begins with the form's name separated with a dot. For example, if your form was named `form` your reference would be something like form.tp1 or form.ZCDisplayKey_tp1
If these Select elements are on a subform then let me know. The syntax can be a bit more challenging and I'd want to write a separate response for that.
--- Sort Order ---
The nice thing about the sort order is that the value is already a number and since your option list is just values 1-4 they are a known offset away from each sort order. You would place the following in the Dynamic Value:
(form.tp1+1) + (form.tp2 + 1) + (form.tp3 + 1) + (form.tp4 + 1)
Notice how I offset each sort order by 1 since the sort orders begin at 0.
--- Key Value ---
Using the key value is going to be a little more "code" but for what it's worth I try to keep things as consistent as possible and for that reason I default to key value 9/10 times even if it's a bit more work. Similarly to the sort order, you'd put the following in the Dynamic Value:
Number(form.ZCDisplayKey_tp1) + Number(form.ZCDisplayKey_tp2) + Number(form.ZCDisplayKey_tp3) + Number(form.ZCDisplayKey_tp4)
A few important takeaways here. The first is that even though your key value looks numeric, it is always stored as text (called a String). To convert, we use the Number() function.
--- Final Considerations ---
In terms of implementation, I recommend adding a Read-Only Element (not a Text Element with read-only attribute) to your form and add the decided upon Dynamic Value. Additionally, add "false" (no quotes, just the word) to the Condition Value so that the element is hidden.
Lastly, I assumed that all 4 fields are required and would hold a valid value. In the event that they are not required then be aware that an unselected element would hold a sort order value of -1 and a ZCDisplayKey/ZCDisplayValue of null or empty string.
As with all forms, make sure to test with all anticipated operating systems and device models to ensure compatibility. Hope this helps and let us know if you have more questions.
Comment actions
Please sign in to leave a comment.
Comments
1 comment