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.

    images/ecj.png
  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.

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

    images/ecj3.png
  4. Click the [...] button next to the 'Lookup definition' property.

    images/ecj4.png
  5. In the Edit Combo Builder from the 'Choices are' selection choose the 'Static' option.

    images/ecj5.png
  6. Enter the choices to appear in the Edit-Combo box.

    images/ecj6.png
    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.

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

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

    images/ecj9.png
  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);
    images/ecj10.png
  11. Run the component in Live Preview. First click the dropdown in the Edit-Combo box, note the static choice selections you defined.

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

    images/ecj12.png

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.

images/ecj13.png

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.

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

    images/ecj15.png
  3. From the 'Lookup type' property dropdown select 'Edit-Combo'.

    images/ecj16.png
  4. Check the [...] button next to the 'Lookup definition' property.

    images/ecj17.png
  5. In the Edit Combo Builder select the Choices are: 'Dynamic'' radio button.

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

    images/ecj19.png
  7. Set the 'Table name' property to be Customers.

    images/ecj20.png
  8. From the 'Fields to display' property select COUNTRY.

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

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

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

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

    images/ecj25.png
  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);
    images/ecj26.png
    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.

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

    images/ecj28.png