CommCare – How to Use Repeat Group to Iterate Over Space-Separated List

commcare

I know that repeat groups can operate in a few different ways:

  1. With a fixed repeat count (the repeat count is an integer loaded in from a hidden variable)
  2. With an unspecified repeat group (the repeat group continues until the mobile worker decides to exit the loop)
  3. With a Model Iteration ID Query (I've seen this used primarily to iterate over cases retrieved from the casedb)

Is there a 4th option that allows you to iterate over a space-separated list of items, and access each subsequent item as a hidden variable inside the repeat group?

Best Answer

A repeat group is capable of iterating over a space separated list with a Model Iteration query, but like any repeat group auto-expansion, this can only occur over a set of values which is fixed when the form opens.

That means the list can't be determined by user input in the form, unless you follow the pattern where the repeat contains an entry for every possible selection, and uses Display Conditions around an inner group to hide elements which aren't chosen.

With those caveats: You can actually accomplish this quite simply, by providing the path to the space separated list as the Model Iteration query itself. Model iterations actually internally operate over a space separated list that they generate by performing a

join(' ', instance('something')/your/iteration/query)

operation on your input. As such, if you provide a query with only one element, the join will just return your space separated list and proceed as usual!

EDIT: Forgot to mention - if you are going to use this method and reference a question inside the form (rather tthan an instance as in my example) it needs to:

  • Be set it using a default value, not a calculation
  • Needs to come before the model iteration loop.