Dynamically Re-populate the Choices in an Edit-Combo using Javascript

Description

Text box controls can be used to create edit combo boxes. The choices shown in an edit-combo can then be dynamically repopulated at run-time using Javascript.

 Re-populate a Simple "Static" Choices based Edit-Combo box.

For a video explanation describing this process watch this video or use the guide below.

  1. In the UX Builder's UX Controls page open the 'Data Controls' menu. Click on the [TextBox] option to add a textbox control to the component.

  2. Highlight the text box control. Scroll down the textbox control's properties list on the right. In the 'Lookup' section check the 'Lookup' property.

  3. Click the dropdown next to the 'Lookup type' property. Select the 'Edit-Combo List' option.

  4. Click the [...] button next to the 'Lookup definition' property.

  5. In the Edit Combo Builder from the 'Choices are' selection choose the 'Static' option.

  6. Enter the choices to appear in the Edit-Combo box.

    If you are using the iOS7 style, setting the 'Width' property under the Window Properties section to something like 1in might be a good idea. Alternatively you can increase the 'Height' property.
  7. Open the 'Other Controls' menu. Click on the [Button] option to add a button to the component.

  8. Highlight the button. In the Button Properties section of the properties list set the 'Button text' property to read 'Set Choices Simple'.

  9. Scroll down the properties list to the 'Javascript' section and click the [...] button next

  10. Click on the 'Text mode' radio button in the Edit Click Event dialog. Add the following code and click Save.

    var _d = ['alpha','beta','gamma'];
    var obj = {Dialog.object}.getControl('TEXTBOX1');
    var newData = _d;
    obj.setData(newData);
  11. Run the component in Live Preview. First click the dropdown in the Edit-Combo box, note the static choice selections you defined.

  12. Click the 'Set Choices Simple' button and then reopen the Edit-Combo box. You should see a different set of selections.

 About 'Static' data

In the example above, if you were to look at the source dialog and looked at the Edit-Combo definition by searching a5.edit you might see something like this.

The static data for the edit-combo is a simple array. When the 'Set Choices Simple' button was defined this array was replaced with another simple array:

['alpha','beta','gamma']

More complex data sources use arrays of objects, each of which can contain their own properties. The Guide below describes how to dynamically repopulate a more complex Edit-Combo.

 Re-populate a SQL based Edit-Combo

  1. In the UX Builder's UX Controls page open the Data Controls menu and click on [TextBox] to add a textbox control.

  2. Highlight the textbox control. In the properties list in the 'Lookup' section check the 'Lookup' property checkbox.

  3. From the 'Lookup type' property dropdown select 'Edit-Combo'.

  4. Check the [...] button next to the 'Lookup definition' property.

  5. In the Edit Combo Builder select the Choices are: 'Dynamic'' radio button.

  6. In the Edit-combo Definition section. Set the Dynamic type property to AlphaDAO and the 'Connection string' to the to Northwind database,

  7. Set the 'Table name' property to be Customers.

  8. From the 'Fields to display' property select COUNTRY.

  9. Set the 'Field to Return' to be 'Country'. Click OK to close the Edit-Combo Builder.

  10. Highlight the textbox control. Open the 'Other Controls' menu and click on the [Button] option to add a button control to the component.

  11. Highlight the button control. In the properties list under 'Button Properties' set the Button text property to read "Set Choices".

  12. Scroll down the properties list to the Javascript section. Click on the [...] button next to the onClick property.

  13. In the "Edit onClick Event" dialog check the 'Text mode' radio button. Add the following code and click save.

    var obj = {Dialog.object}.getControl('TEXTBOX1');
    var newData = [
         {COUNTRY: 'Alpha Corp'},
         {COUNTRY: 'Beta Corp'},
         {COUNTRY: 'Gamma Corp'}
         ]
    obj.setData(newData);
    Here the array of objects containing the COUNTRY property, that was defined in the 'Customers' table's Country field, is being replaced by an array of objects defined in Javascript.
  14. Run the component in Live Preview. First click on the Edit-combo drop down to see the list of countries from the 'COUNTRY' field.

  15. Now click the 'Set Choices' button and click on the Edit-Combo again. The selection should be different.