Some app-building tricks

Setting up iteration over the product list

Why?

This will allow you to iterate over your product list to either read or write values for each of them

How?

In order to iterate over a list of products, you need to set up a repeat group over the product list

This is configured in the “products” repeat.

 

  • Click on the repeat → Advanced → Model Iteration ID Query

  • Click “Edit”

  • Set the following:

  •  

  • Click “Save”

  • This is the exact same for all product repeats.



Extra: If you want to run the product only over a define list of products, you can use the Query Expression to filter the list.

For example the following expression will run the iteration over product of a defined program: 

instance('products')/products/product[./program_id=xxx]/@id

How to refer to the product in a repeat? The use of current()

Referencing the ID of a product in the repeat can be tricky because it’s not obvious how many “../” expressions should be in the path.

This is a complicated question that involves understanding XML and XPath references, and particularly how they work in our tools.

However, you can use these general guidelines to help. 

 

Question you are referencing

Way to reference

Display of the repeat group itself

current()/@id

Normal question directly in repeat

current()/../@id

Normal question in a group inside repeat

current()/../../@id

For each additional group inside repeat

add an additional ../ to the path

 

The CommCare Supply transaction questions require special references. These are described below. These are all relative the a normal question in the same group, so additional “../” elements must be added to these paths when using the transaction questions inside groups.

 

Question you are referencing

Way to reference (assuming no groups)

Product ID in a transfer question directly in repeat

current()/../../../@id

Product ID in a balance question directly in repeat

current()/../../../@id

For each additional group above the question

add an additional ../ to the path



Showing all products of a repeat group in one page

When using repeat groups to iterate over product list, if you show a label or ask a question, each product will be shown in a separate page.

Depending on the number of products, displaying them all in one page can be more user friendly.

To do so just drag your repeat group in a question list:

 

 

 

 

 

 

 

 

Summing data across all products

 

 This is standard app-builder stuff, and can be done using the sum() function.

 Outside the repeat group do a sum of a question inside the repeat group.

 For example: sum(/data/products/item/data_product_payment) will sum the values of each product data_product_payment.

 PS: the additional hidden word “item” in the path that is inserted due to the model iteration structure.