Lesson 4: Populate a combo box dynamically
In this section, you enhance the search page by listing the possible choices for the customer's state in a combo box.
To make searching as easy as possible for the users, you should try to prevent user error by simplifying the decisions that the user must make wherever possible. In this section, you learn how to make the search page easier to use by replacing the State input field with a combo box. This combo box lists only the states that are represented by at least one customer record in the database, preventing the user from having to guess which state to use.
First, you need to add a function to your library that retrieves every state represented in the database. This function is simpler than the other functions in the library, because you need to retrieve only one column from the database. In addition, there are no input parameters for the search, only an output array holding the list of states. You could use an array of customer records for the state information, but an array of strings is simpler to work with because you don't need the rest of the fields in the record.
- Open SearchLibrary.egl.
- Add the function shown in listing 15 to the library:
Listing 15. Get states
function getAllCustomerStates(listOfStates string[])
customers Customer[0];
counter int;
get customers with
#sql{
select EGL.CUSTOMER."STATE"
from EGL.CUSTOMER
group by EGL.CUSTOMER."STATE"
order by EGL.CUSTOMER."STATE" asc
};
listOfStates.removeAll();
for (counter from 1 to customers.getSize() by 1)
listOfStates.appendElement(customers[counter].State);
end
end
|
- Save the file.
- Generate the library.
Here are some technical notes about the getAllCustomerStates() function that you just added:
-
This function accesses the customer records from the database in the same way as the
getAllCustomers()function. The major difference is that thegetAllCustomerStates()function selects only the STATE fields instead of every field in the Customer table. -
The
group bySQL command groups the results by state, so that each state is listed only once in the results. -
The
order bySQL command puts the results in alphabetical order (theasckeyword indicates ascending order). -
The
forloop moves only the state field from the records into an array of strings.
Here is the complete code (egl_code02-01-04a.html, zipped up and available in the Downloads section) for the SearchLibrary.egl file. If you see any errors marked by red X symbols in the file, make sure that your code matches the code in the egl_code02-01-04a.html file.
- Return to the customersearch.jsp page.
- Right-click inside the customersearch.jsp page and click Edit Page Code.
-
After the line
andOr char(3);, add the line of code shown in Listing 16.
Listing 16. Add the customerStates string
customerStates string[0];
|
- This variable holds the list of states returned by the function in the library.
-
Add a blank line after
Function onPreRender(). Then, add the line of code shown in Listing 17 on the blank line.
Listing 17. Get the states
SearchLibrary.getAllCustomerStates(customerStates);
|
- Save the file.
- Generate the file.
The customersearch.egl file looks like that shown in Figure 13 when you are done (some functions are compressed, indicated by a plus sign in the left margin of the page):
Figure 13. Updated code
Here are some technical notes on the code that you just added:
-
The
customerStatesarray holds the list of states represented by at least one customer in the database. -
The line you added to the
onPreRenderfunction sends thecustomerStatesarray to thegetAllCustomerStatesfunction in the library, populating the array with the list of states.
Here is the complete code (egl_code02-01-04b.html) for the customersearch.egl file, zipped up and available in the Downloads section. If you see any errors marked by red X symbols in the file, make sure your code matches the code in the egl_code02-01-04b.html file.
Adding a combo box that is populated dynamically is more complicated than adding a JSF control that has predefined values, such as the radio button group you added in the previous section. This combo box must be bound to two pieces of EGL data:
-
The
customerStatesarray, which provides the list of options for the combo box. -
The
searchTerms.Statevariable, which holds the user's selection from the combo box.
Perform the following steps.
- Return to the customersearch.jsp page.
- Click the STATE input field to select it and press Delete. The input field is removed from the page.
- From the Palette, open the Enhanced Faces Components drawer.
- Drag a Combo Box item to the page, and place it where the STATE input field was.
- In the Page Data view, expand searchTerms - Customer.
- Under searchTerms - Customer in the Page Data view, drag State - State onto the combo box.
- Click the combo box to select it.
-
Open the Properties view. In the Properties view, note that the Value field is set to
#{customersearch.searchTerms.State}, indicating that the value of the selection in the combo box is placed into theStatefield of thesearchTermsrecord. -
In the Properties view, click Add Set of Choices, which is at the far right of the view, near the table of choices. A new item is added to the list of choices with the label
<selectitems>and a default value. The<selectitems>label is a JSF tag that represents multiple options, rather than a single static label. In other words, the combo box will use the values you specify in the Value column for both the labels in the combo box and the values that the labels represent. -
Next to the
<selectitems>label, click the Select Page Data Object button in the Value field. The Select Page Data Object window opens. - In the Select Page Data Object window, click customerStates - string[].
The Select Page Data Object window looks like that shown in Figure 14:
Figure 14. Select page data object
ALT:
-
Click OK. Now the choices in the combo box come from the
customerStatesvariable, while the selected state in the combo box is put into thesearchTermsvariable. - Save the page.
- Test the page.
You have created a combo box on your Web page that creates a list of search parameters.
In this section, you learned how to do the following things:
- Add the code for a limited search to the library
- Add code to the JSF Handler to call the revised search function
- Add a combo box to the Web page
- Bind the revised search function to the combo box
Now you are ready to begin Lesson 5: Customize the search results.





