CommCare – How to Join Multi-Select Lookup Table Values with Commas in a Label

commcare

I have a multi-select question that pulls its values from a lookup table. Once the user has selected some of these options, I want to show them to the user in a comma-separated list.

The lookup table has a 'value' column, which is a snake_case version of the 'name' column which has a human-readable version of the item the user is selecting.

I would like to display the human-readable version of all of the items the user has selected in a label.

I've tried 2 approaches:

1) set the 'value field' of the lookup table question to be the 'name' column then use replace(/path/to/question, " ", ", ") to show the results comma separated. Unfortunately this doesn't work because some of the names have spaces in them, and these are getting replaced by commas.

2) Use a repeat group to loop over each of the selected items in the question and compile a label using selected-at(/path-to-question/, position(..)). This doesn't work because the label doesn't update if the user deselects options they previously selected.

Is there another way of doing this?

Best Answer

Are you able to change how the values are stored int he lookup table? If you could change the spaces in the values to "_" you'd be able to use replace as you want to. I think it's bad practice to store spaces as values anyway ... when you export that data you'll have the same issue with separating out the responses.

If you change to "_" in your data you should be able to nest the replaces to display the values without "_"'s to the users:

replace(replace(path/to/question, " ", ","), "_", " ")

That would convert:

"option_1 option_2 option_3" to "option 1, option 2, option 3."

Related Topic