This came up on an internal mailing list:
We have a secondary data source that holds a list of entries; part of the data in the list - a name field - is used to populate the items in a listbox in the view. When the user selects an item in the list, I want the rest of the data from that entry to be used to populate the form's main data source.
Great scenario! It actually can be done with a combination of rules and filters (though I admit it’s not obvious), so you don’t have to write code for it.
For this example, I will assume you have a form with:
- A secondary data source with repeating items (for example, a list of people each with an ID, Name and Age)
- A dropdown that lists the values in the data source (e.g. displays the name of each person and uses the ID as the value)
- A set of nodes in your main data source that should be auto-populated when the dropdown changes (e.g. An age textbox that gets the age of the person selected in the dropdown)
What you’ll need to do is create a rule on the dropdown. The rule will set the value in the textbox by filtering the corresponding value in the secondary data source down to one that matches the dropdown selection.
Here’s how to do it:
- Double click the dropdown to open its Properties Dialog (it can be bound to a node in the main data source or in a secondary one, it doesn’t matter)
- Click the Rules button
- Click Add to add a rule
- Click Add Action
- For the Action, select Set a field’s Value
- For the Field, select the selectedAge node or whatever node you want to set automatically
- For the Value, click the Fx button
- In the Formula dialog, click Insert Field or Group
- In the Data Source dialog, select the secondary data source and then the age node or whatever node you want copy over
- Click the Filter button
- In the Filter dialog, set the filter to “secondary-data-source-ID is equal to main-data-source-ID”
- OK out of all dialogs, you’re done.
Note that if your dropdown is inside a repeating section/table, then you will need to manually adjust the value in the formula dialog after step 11 to use the “current()” function so the filter works correctly.