These steps repeat the actions that were taken in the video above.
Instructions
These steps are for setting up a specific kind of choice filter, where choices in a select_one field are filtered based on the choices selected in a select_multiple field. But, there are lots of other ways to set up choice filters.
Online form designer
Part 1: Add field used for filtering
This part is optional. You can also use a different field for the choice filter.
- In the online form designer, click a plus on the left.
- Give the field a label.
- Click "select_multiple".
- Click Configure.
- In the field editor that opens, give the field a name.
- Near the top, for choice list, click Create a new choice list.
- Give the choice list a name.
- In the choice box, give the choice a label, value, and filter. For this workflow, the filter should be the same as the value.
- Click Save to save the choice.
- For each additional choice you would like to add, click Add choice, and repeat steps 8-9.
- Click Save now to save the field.
Part 2: Add field with choice filter
- In the online form designer, click a plus on the left.
- Give the field a label.
- Click select_one.
- This can also be a select_multiple field, but for this workflow, it will be a select_one field.
- Click Configure.
- In the field editor that opens, give the field a name.
- Near the top, for choice list, under Use existing choice list, click the dropdown menu, and select the choice list created earlier in part 1, step 7.
- You can also use a different choice list, but for this workflow, we will use the same choice list.
- Scroll down, and click More options.
- Scroll to the very bottom.
- For choice_filter, click the text entry box to the right.
- Enter your choice filter expression based on the field from part 1.
- Click Save now.
Spreadsheet form definition
Part 1: Add choice list
- Go to the choices sheet.
- In an empty row, add a choice. Give it a list_name, value, label, and filter. For this workflow, the filter should be the same as the value.
- Repeat step 2 for each choice you would like to add. The list_name for each choice in this choice list should be the same, but each value and label should be unique.
Part 2: Add field used for filtering
- Go to the survey sheet.
- In a blank row, add a new field with a type, name, and label. For the type, it should be "select_multiple", followed by a space, followed by the list_name from part 1, step 2 (e.g. "select_multiple crops").
Part 3: Add field with choice filter
-
- On the survey sheet, in a blank row below the last field, add a new field with a type, name, and label. For the type, it should be "select_one", followed by a space, followed by the list_name from part 1, step 2 and part 2, step 2 (e.g. "select_one crops").
- Give the field a choice_filter expression based on the field from part 2.
FAQs
Why use the selected() function instead of the =
comparison operator?
In the video above, for the choice_filter, it does not use this expression:
${crops_grown} = filter
But this expression:
selected(${crops_grown}, filter)
A select_multiple field stores its value as a space-separated list. So, for the field "crops_grown", if choices "Bananas" and "Carrots" were selected, the field would have this value:
2 3
The = checks if the value on the right is exactly equal to the value on the right. So, if "crops_grown" has a value of "2 3", and it is checking the choice with a filter value of 2, it be doing this check:
2 3 = 2
Even though 2 definitely exists in the list, that list is not exactly equal to 2, so the expression is false, and choice 2 ("Bananas") will not appear.
Instead, use the selected() function, which checks if the filter value simply exists in the list, instead of being exactly equal to the list.
Do choice filters have to use a field reference to a select_multiple field?
Nope! A select_multiple field was used for this example, but you can use any other field. In fact, you don't even have to use a field; if you want to use a specific choice list, and you want to hide the same choices every time, you can use a static choice filter, like this:
filter = 1
With that choice_filter, only choices with a filter value of 1 will appear. This kind of choice_filter by itself is not common, but it has its uses in more complex choice filters to make sure certain choices always appear, such as "Don't know" and "None of the above".
Do filter values have to be the same as the choice value?
Nope! The filter value can be anything. The example workflow above happened to need filter values that are the same as the choice values, but they don't have to be the same.
Can my choices have non-numeric values?
Yes, and some data analysts may prefer non-numeric choice values, since they are easier to understand at a glance. However, some data analysis applications do not support non-numeric choice values, so consider your whole workflow.
We strongly recommend against using spaces in choice values, since select_multple field values are stored as space-separated lists, and a space in a choice value could be read as two choices instead of a single choice.
Can filter values be dynamic (use field references)?
No, choice filter values must be static; they cannot be field references, and they cannot use functions. Choice values cannot be dynamic either. For choices, only the choice label and image can be dynamic.
You can definitely still use field references in your choice_filter expressions on the survey sheet, just not the actual filter values on the choices sheet.
Can I filter pre-loaded choice lists?
Yes, but not using the choice_filter property. If you would like to filter pre-loaded choices lists, use the other parameters in the search() function, or use the preload-filter field plug-in.
Further Reading
To learn more about choice filters, check out our Guide to choice filters.
Do you have thoughts on this support article? We'd love to hear them! Feel free to fill out this feedback form.
0 Comments