Lesson 5: Customize the search results
In this section, you learn to make a more complex data table to display your search results.
Until now, each JSF component you have added to Web pages has been bound to data from a single database table. If you are using a complex relational database, you may want to work with data from more than one table at a time.
In this section, you customize the results by displaying data from both the Customer table and the State table. In this way, the results show both the customer's name (from the Customer table) and the full name of the customer's state (from the State table) instead of the two-letter abbreviation. You also manipulate the results by combining the customer's first name and last name into a full name field. The resulting data table looks like this:
Figure 15. Combine information from two data tables
The easiest way to create a customized data table like this is to create a customized EGL record that represents a single record in this data table. Then, you create an array of these records that is bound to the data table. The customized EGL record you create in this section has the following three fields:
-
The
emailfield, which holds the customer's e-mail address from the Customer table. -
The
fullNamefield, which holds the customer's combined first and last name from the Customer table. -
The
Statefield, which holds the full name of the customer's state. To get the full name, the search function cross-references the customer's state abbreviation from the Customer table with the list of abbreviations and state names in the State table.
Perform the following steps.
- Open SearchLibrary.egl.
- Add the function shown in Listing 18 to the library.
Listing 18. Get data from the State table
function getOneState(state Statetable)
get state;
end
|
-
The
stateTablerecord is defined in the file Statetable.egl, so if you did not use code completion to enter that function, you must import that record. -
Add the following statement to the SearchLibrary.egl file, immediately after the
package libraries;line, as shown in Listing 19
Listing 19. Import the record
import eglderbyr7.data.Statetable;
|
-
Add the code shown in Listing 20 to the file, just after the final
endstatement in the library.
Listing 20. Define displayNames for the strings
Record customizedResult type basicRecord
fullName string
{displayName = "Full Name"};
email string
{displayName = "Email Address"};
stateName string
{displayName = "State"};
end
|
Note: Because the library itself can not contain Record definitions, you must add the customizedResult record definition after the end statement that closes the library.
- Save the file.
- Generate the file.
Here is the complete code (egl_code02-01-05a.dita, 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-05a.dita file.
Add code to the page code file
- Return to the customersearch.jsp page.
- Right-click inside the customersearch.jsp page and click Edit Page Code.
-
Add the following variable definition to the JSF Handler, after the
customerStates string[0];line, as shown in Listing 21.
Listing 21. Define the allRecords variable
allRecords customizedResult[0];
|
- This variable represents the new search results, based on the customized record that you just created.
-
If you did not use code completion to insert this line of code, add the
importstatement shown in Listing 22 to the JSF Handler, below thepackage jsfhandlers;line.
Listing 22. Import statement
import libraries.customizedResult;
|
- Add the function shown in Listing 23 to the JSF handler.
Listing 23. Generate results function
function generateCustomResults(passedResults Customer[])
allRecords.removeAll();
oneRecord customizedResult;
counter int = 1;
state Statetable;
//loop once for each search result returned
while (counter <= (passedResults.getSize()))
oneRecord.fullName = passedResults[counter].FirstName ::
" " :: passedResults[counter].LastName;
oneRecord.email = passedResults[counter].EmailAddress;
state.StateAbbrev = passedResults[counter].state;
SearchLibrary.getOneState(state);
oneRecord.stateName = state.StateName;
allRecords.appendElement(oneRecord);
counter = counter + 1;
end
end
|
-
This function assembles the customized search results. You must call this function at the end of the
searchFunction()function. -
Add the line of code shown in Listing 24 immediately before the
endstatement that closes thesearchFunction()function in the JSF Handler:
Listing 24. Generate results
generateCustomResults (searchResults);
|
- Save the file.
The new function you added to the JSF handler assembles the customized search results by following these general steps:
- Assemble the customer's full name from the first and last name.
- Get the customer's email address.
- Get the abbreviation of the customer's state.
- Look up the state name that matches the abbreviation.
-
Add the full name, email address, and state name to the
allRecordsarray.
Here is the complete code (egl_code02-01-05b, zipped up and available in the Downloads section) for the customersearch.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-05b.html file.
Create the customized data table
- Return to the customersearch.jsp page.
-
If you want to remove the old search results, you can delete the old data table. These steps are optional:
- Click anywhere in the old search results table to set the cursor focus there.
- Press the down arrow key. The entire data table is now selected.
- Press Delete. The data table is removed from the page.
- From the Page Data view, drag allRecords - customizedResult[] onto the page, just below the old data table location. The Insert List Control window opens.
- Click the radio button next to Displaying an existing record (read-only).
- Click Finish. The new data table is created on the page.
- Save the page.
- Test the page.
Now when you search for a customer, you see the customer's full name, e-mail address, and full state name in the data table. The page looks like that shown in Figure 16.
Figure 16. Search with full results displayed
You have customized your search results by combining the first name and last name fields into a full name field, and by translating the two-character code from the state field into the full name of the state.
In this section, you learned how to do the following things:
- Add a function to the library that customizes results
- Add a record to the library file, outside the library itself
- Add the new customized results function to the JSF Handler
- Place a new results table on the Web page
- Bind the new results function to the table





