Conditional Display in CommCare – How to Show Questions Based on Lookup Table Answers

commcare

How can I conditionally display labels depending on the answers selected in a series of multiple choice lookup tables (similar to the filter with Xpath to do this with lookup tables)?

Can I use the answer from a lookup table as a display condition for a label in my app, or would the best option to reference my lookup table in my form be to use < instance id="random_questions" src="jr://fixture/item-list:random_question"> in a hidden value and then use that hidden value in the display condition of my label?

Best Answer

You can indeed. It's a little difficult to explain generically, so I'll use the example from this wiki page. Let's say you have a question (called "Select District") where the choices come from a lookup table:

| field: id | field: name | field:state_id | user 1     | group 1             |
|-----------+-------------+----------------+------------+---------------------|
| ah        | Ahmedabad   | gujurat        | supervisor | Gujurat Users       |
| ja        | Jamnagar    | gujurat        | supervisor | Gujurat Users       |
| agra      | Agra        | up             | supervisor | Uttar Pradesh Users |
| faizabad  | Faizabad    | up             | supervisor | Uttar Pradesh Users |

Your question will probably have "Value Field" set to "id" and "Label Field" set to "name". When the user then selects, for instance "Jamnagar", the value of that question will be "ja". You can then use that to get more information from the selected row wherever else you may need it. Here's how you reference the name of that row:

instance('project')/project_list/project[id = /data/select-district]/project_name

If you have a question that you only want to display for districts where "state_id" is "gujurat", you can add a display condition like:

instance('project')/project_list/project[id = /data/select-district]/state_id = gujurat