This article is part 3 of our series on repeat groups. See the bottom of this article for the other parts in this series.
This is an advanced topic. You do not need to understand this to understand the basics of referencing repeated data. You can always skip this for now, and return later.
This section is accompanied by this sample form. For help deploying it to your server to try it out, check out our support article Deploying form definitions.
When a repeat group is inside another repeat group, it is called a nested repeat group.
The nested repeat group (also known as the "inner group") will have its own repeat_count, and the whole nested repeat group will repeat based on the outer repeat_count. For example, if the outer repeat group has a repeat_count of 4, and the inner repeat group has a repeat_count of 3, then the inner repeat group will have a total of 12 repeat instances.
Just like regular repeated fields, you can use functions on nested repeated fields.
Table of Contents |
1. Retrieving and checking all nested data
With most functions that retrieve repeated data, they will check ALL values, ignoring which inner or outer repeat instance it is in.
For example, let's say there is an outer repeat group with a repeat_count of 4, which has a nested, inner repeat group that also has a repeat_count of 3. That inner repeat group contains a select_one field called "crop_grown", and a calculate field called "crop_name" that retrieves the label of the selected choice. When filling out the form, the enumerator selects these choices (each row is an instance of the outer repeat group, the comma-space-separated items are each instance of the inner repeat group):
- Dates, Apples, Carrots
- Carrots, Bananas, Dates
- Bananas, Apples, Apples
- Carrots, Bananas, Apples
Then, there is a calculate field with this calculation:
join(', ', ${crop_name})
That field would return this value:
Dates, Apples, Carrots, Carrots, Bananas, Dates, Bananas, Apples, Apples, Carrots, Bananas, Apples
As you can see, all values are joined into a single list. This is demonstrated by "all_crops" in the sample form.
2. Retrieving a specific nested value with indexed-repeat()
The indexed-repeat() function can also be used to retrieve specific nested values. Simply add an additional repeat group reference and index for each nested repeat group.
For example, if you would like the value of "crop_name" from "plot_rep" index 2 and "crop_repeat" index 4, you would use this expression:
indexed-repeat(${crop_name}, ${plot_rep}, 2, ${crop_repeat}, 4)
Tip: The order of the repeat group and index pairs do not matter. So, this:
indexed-repeat(${crop_name}, ${plot_rep}, 2, ${crop_repeat}, 4)
Is the same as this:
indexed-repeat(${crop_name}, ${crop_repeat}, 4, ${plot_rep}, 2)
Just make sure the indexes are to the right of the correct repeat group!
2.1 Retrieving nested values inside the outer repeat group
Whenever you want to reference a nested repeated field outside of its repeat group, make sure you include all repeat groups, even if you are already in one of those repeat groups.
For example, let's say you want to add a calculate field inside of "plot_rep", but outside of "crop_repeat", and you want it to retrieve the first instance of the repeated field "crop_name" inside "crop_repeat". You would use this expression:
indexed-repeat(${crop_name}, ${plot_rep}, ${plot_index}, ${crop_repeat}, 1)
Here, "plot_index" is the current index of the outer repeat group. Notice how even though that expression is inside the outer repeat group "plot_rep", it still uses a reference to that repeat group. This is demonstrated by "first_crop" in the sample form.
More on repeat groups
- Repeat group basics: What repeat groups are, and how to use them.
- Using and referencing repeated data: How to use repeated data in other parts of your form.
- Nested repeat groups (advanced): How to use a repeat group inside of a repeat group.
- Cookbook: Various tools and examples of using repeat groups and functions you can use in your own forms to retrieve and generate the data you need.
Do you have thoughts on this support article? We'd love to hear them! Feel free to fill out this feedback form.
0 Comments