How To: Add Lookup Information to a Form
Use the Lookup control to select a value from a dialog window containing a list of values. For example, you may have a SmartObject that contains a list of items. As part of your form you want to show a subview with the list of items (lookup information), and then select an item to populate data on the form.
Runtime example of the Region lookup list
Try it yourself!
Scenario
Your organization uses a form to capture customer information. To make it easier to complete the form, you want to show a dialog box so that the form user can search and select a region for the new customer.
Steps
For this scenario, you begin by building two SmartObjects called Customer and Region that stores customer and region details. Next, you use the Customer SmartObject to create an item view and from the Region SmartObject generate a list view. You use the item view to capture the data on the form and the list view as the lookup reference. You edit the Customer item view and change the Region ID text box to a lookup control. You then configure the Region ID field to use the Region SmartObject as the data source for the lookup list. You add a rule to the Customer item view to open the Region list view in a subview, and add another rule to populate the Region ID field with the selected item when you double click it. Finally, you add some data to the Region SmartObject and test the solution.
Prior to working through this how-to, you should know how to work with the category system and add elements such as SmartObjects and views. If you do not know how to build the basic application elements, see: Getting Started (with K2 Designer).
Setup Steps
The following steps set up the scenario from start to finish. If you want to skip the setup steps and view the topic step, go to Edit and configure the Region ID field.
-
Create two new SmartObjects called Customer and Region.
- From the K2 Designer, create the Customer SmartObject as shown below.
- Create the Region SmartObject as shown below.
- From the K2 Designer, create the Customer SmartObject as shown below.
-
Generate two views, an Item view from the Customer SmartObject and a List view from the Region SmartObject.
- From K2 Designer, right click the Customer SmartObject, select Generate Views and check the Item check box to generate the Customer Item view. Click OK.
- To generate the second view, right click the Region SmartObject, select Generate Views and check the List check box to generate the Region List view. Click OK.
- From K2 Designer, right click the Customer SmartObject, select Generate Views and check the Item check box to generate the Customer Item view. Click OK.
Edit and configure the Region ID field
In this section, you edit the Customer Item view, change the Region ID text box to a lookup control, and specify the Region SmartObject as the data source. On the Customer Item view, add two rules: One rule to open the Region list view in a subview when you click the Region lookup and another rule to transfer data to the Region ID when you double click an item on the Region List subview.
-
Change the Region field to use a lookup control and edit the control's properties
- From the K2 Designer, locate the Customer Item view and select Edit.
- Select the Region ID field and click the Change Control icon.
- Select Lookup and click OK.
- You changed the Region ID field from a text box to a lookup field.
- Next you configure the data source for the lookup field. Select the Region ID field, then click the ellipsis next to the SmartObject view property.
- Click the ellipsis next to SmartObject, browse to and select the Region SmartObject. The Method, Value and Display is populated as shown below. Click OK.
- Still on the Customer Item view, select the Rules tab and click Add Rule.
- In the search field type when a control, locate the Control Events section, and click When a control on the View raises an event.
- Select RegionID Lookup for the control and Clicked for the event.
- From the Actions tab, type open a in the search field, locate the Subview Interaction section, and click Open a subview.
- Select the Region List view. This rule opens the Region List view in a subview when you click the RegionID Lookup control. Click OK.
- Note how the Region List subview executed Initialize rule is automatically added. This rule populates the Region List subview.
- Click Add Rule again. Type When a subview executes, locate the Subview Events section, and click When a subview executes a method.
- Select the List Item double click option.
- From the Actions tab, type transfer , locate the Data Transfer section, and click Transfer data.
- Click configure, then expand the Region SmartObject in the Context Browser. Drag the RegionID property into the RegionID Lookup field. Click OK.
- Still in the Actions tab, type close, locate the Subview Interaction section, and select Close a subview or subform. This rule transfers data from the Region List subview to the Customer View when an item is double clicked on the subview, and then the Region List subview closes. Click OK to save the rule.
- The Customer Item view rules should look like the image below. Click Finish to save and exit the view.
- From the K2 Designer, locate the Customer Item view and select Edit.
-
Generate an editable list view, add data to the Region SmartObject, and generate the Customer form.
- From K2 Designer, right click the Region SmartObject, select Generate Views and check the Editable List check box to generate the Region Editable List view. Click OK.
- Select the view and click Run.
- Add data to the Region SmartObject as shown below.
- From K2 Designer, right click the Customer Item view, select Generate Forms. On the Generate Forms page, type Customer as the form name and click OK.
- From K2 Designer, right click the Region SmartObject, select Generate Views and check the Editable List check box to generate the Region Editable List view. Click OK.
-
Test the solution by running the Customer form, clicking the Region ID field and selecting an item from the subview.
- From K2 Designer, select the Customer form and click Run.
- To test the Region lookup control, click the ellipsis in the Region ID field.
- The Region List opens as a lookup list. Double click Region 2 to select it from the list.
- Region 2 gets added to the Region ID field.
- From K2 Designer, select the Customer form and click Run.
Review
You learned how to use the Lookup control on a form to show a subview with the list of items (lookup information) and then select an item to populate a field on the form.