...
The 1st rule "Load Products" populates both the visible and hidden dropdowns with options from a database.
Code Block | |
---|---|
javascript | /*member description productId resultSet */ var x; if (form.load) { eval('x=' + http.get('http://localhost:8082/database/products')); var opts1 = []; var opts2 = []; for (var i=0; i < x.resultSet.length; i++) { if (x.resultSet[i]) { opts1[i] = x.resultSet[i].description; opts2[i] = x.resultSet[i].productId; } } Products.options = opts1; PID.options = opts2; Products.value = opts1[0]; // default to 1st product option PID.value = opts2[0]; } |
Finding a Selected Options Index
The 2nd rule Select Product ID keeps the hidden PID dropdown syncronized with the visible Products dropdown.
...
Here is another rule that dynamically populates both the product choices and product ID dropdowns. This rule calls a REST Service which returns an object rather than the resultset returned by the database connector as shown above. See the section on dynamic content for more details.
Code Block |
---|
/*member ids products */ var x; if (S.value.length > 0) { eval('x=' + http.get ('http://localhost:80828182/products/?category=' + S.value)); P.options = x.products; ID.options = x.ids; } |
Synchronized Selects
The Product Search example above is often used in conjunction with a hidden select control. Imagine that your database table contains a list of products. Each product has product description also a unique product ID. The user needs to select a product from a dropdown on your form. You want to populate the dropdown with the product descriptions. The users do not need to see or know the product IDs but you need to use the ID as the key into the database for other selects. To do this add another hidden dropdown to the form and populate it with the IDs. This example has a visible dropdown name Products and an invisible dropdown named PID. See the rule above that populates these dropdowns dynamically from the database.
...