Dynamic Dropdown Lists

Text Dropdown Lists

While creating either static dropdowns or data-populated dropdowns is simple, there may be times when you want the contents of the dropdown to be generated dynamically from business logic. You can do this by creating a Generation Script under the dropdown.

First, go to the Application record and then select Model in the drilldown menu (Hamburger icon). Second, while in the Model view, select Dropdown Lists in the drilldown menu. Create your dropdown list there.

Instead of entering members for your dropdown, go to the Generation Script in the drilldown menu. You can enter a script here. The script must return a list of strings. You can create that list using the NewList() function, then using add "item" to #list as a way of adding items to the dropdown list. At the end of the function, use "return #list" to return the list you created.

 var #list = NewList()
 add "Option 1" to #list
 add "Option 2" to #list
 return #list
 

Then, editing your field, you'd select the central Dropdown list that you created:

This function will be invoked every time the form is displayed that contains a field using this dropdown.

When the script is invoked, the &path variable will be set to the place where the form was displayed.

Link Dropdown Lists

You can also create a link dropdown list dynamically. To do this you'd create a regular link field, but in the Advanced area, in the DbfScript Link field, you'd run a plugin function. This plugin function should then return a list of records. The title of the record will be used for the display, and the path of the record will be used as the link.

Keep in mind that there will be no paging for this list, it will be displayed in one list regardless of the number of items returned. Because of this you should keep the number of records returned preferably below 100 for performance and manageability reasons.

For example, you could create a dropdown link field that points to a Customers tab. In the Advanced / DbfScript Link, you can enter in this to make it run a function:

RunPlugin("Functions:GetCustomers", &path, record () )

 This will have it run a plugin function in the module Functions in order to retrieve the list of records to display.

This function could have a script like this:

var #customers = load(all &root/Customers below &root with top 50)
 return #customers

 In the above example, we would be loading customers throughout the whole system (even in subforms) into one list.


Next Topic:
v4.2.0.956 (beta)
Up Since 2/29/2024 12:02:23 AM