This page will walk through an example of setting up a form that both displays conditional options in lists (list of districts from chosen state) and has options that are specific to each user.
First setup the following two tables using the Creating and Updating Lookup Tables page. You can use whichever mobile workers or groups that you have in your project.
|field: id||field: name||user 1||group 1|
|up||Uttar Pradesh||supervisor||Uttar Pradesh Users|
|field: id||field: name||field: state_id||user 1||group 1|
|agra||Agra||up||supervisor||Uttar Pradesh Users|
|faizabad||Faizabad||up||supervisor||Uttar Pradesh Users|
Our sample creates two tables (state and district). The district table has three fields (id, name and state_id). The state_id will be used to filter the list of districts based on the selected state. The following image shows our final setup.
To create a select list based on a lookup table, first add a single select question to your form.
In our example, we'll add two single select questions, "state" and "district".
Now go to "Lookup Table Data" and select the correct lookup table and value and label fields. When inputting the value and labels an autocomplete widget will show you valid options for these fields.
If you were to test this application, you'd see the first question displays a list of all states and the second question displays a list of all districts (irrespective of the selected state). To filter the district list, we need to add a filter condition to the itemset.
To filter an item set, you can use the 'Filter Option' under 'Lookup Table Data' (similar to the expressions used when defining Display Logic or Calculations in your form). In our example, we'll filter the District item list based on the selected state. Our district table contains a field called "state_id". The values in this match the value specified for the state question.
The left option will show an autocomplete widget to help you pick a lookup table attribute and the right option will allow you to drag a question from the question tree.
In order to test the conditional select options and other logic works in the application, you must sign on as a mobile user and submit 'real' data to the server. If you see an error when opening the form that states "Could not find an appropriate fixture for src: jr://fixture/item-list:table_name" you have not properly setup your lookup tables.
If you run into issues testing your app with lookup tables, try these two troubleshooting options:
Check to see if the table is visible to your mobile user: To check whether or not the lookup table is visible to your user, go to this link: http://www.commcarehq.org/hq/admin/phone/restore/?as=[mobile username]@[domain]&version=2.0 If the table is visible to your user, you should see the table appear in the XML payload.
Clear user data: After you sync your phone if the lookup table still is not working, you may need to clear user data and log-in again. To do this, go to the login screen and click on Settings. After the Settings screen shows up, click the menu button, and select the first option, "Clear User Data." This will clear the username and force you to re-enter login credentials. Once you log in again, the new or edited table should be accessible to your user.
Large lookup tables may cause an error on the mobile device stating "get field slot from row 0 col 1 failed." This is due to the amount of memory allocated to the lookup table on the mobile device. The lookup table is larger than the available memory. If you get this error, a common work around is to split the table into multiple tables. This can be completed by adding a "more" option to the bottom of your first lookup table and performing a conditional question to display the results of the second lookup table if "more" is selected. For example, there are more than 3,600 Village Development Committees (VDC) in Nepal across 75 districts. One user created a conditional select option to display the VDCs within the selected district. However, the VDC list was too large to display in one lookup table. The user created three lookup tables that each supported the VDCs of 25 districts. Three VDC questions were created with conditional criteria that displayed the VDCs from one of the three lookup tables. Finally, a calculated question was created to determine which VDC was selected from the three VDC questions.