Chapter 10. Rich inputs
This chapter details rich components for user input and interaction.
10.1. <rich:autocomplete>
The
<rich:autocomplete> component is an auto-completing input-box with built-in Ajax capabilities. It supports client-side suggestions, browser-like selection, and customization of the look and feel.
The auto-complete box is a standard JSF
UIInput control with added validation.
10.1.1. Basic usage
The
value attribute stores the text entered by the user for the auto-complete box. Suggestions shown in the auto-complete list can be specified using one of two different methods:
- The
autocompleteMethodattribute points to a method which returns a list of suggestions according to a supplied prefix.Note
The prefix is normally ignored inclientandlazyClientmodes. In these modes, the component requests the suggestion list once only, and performs filtering on the client. - The
autocompleteListattribute points to a collection of suggestions.
Example 10.1. Defining suggestion values
- Using the
autocompleteMethodattribute <rich:autocomplete value="#{bean.state}" autocompleteMethod="#{bean.autocomplete}" />
The<rich:autocomplete>component uses thebean.autocompletemethod to provide suggestions, based on the entered prefix.- Using the
autocompleteListattribute <rich:autocomplete value="#{bean.state}" autocompleteList="#{bean.suggestions}" />
The<rich:autocomplete>component retrieve the suggestion list frombean.suggestions.
10.1.2. Submission modes
Use the
mode attribute to determine how the suggestion list is requested:
- The
clientsetting pre-loads data to the client and uses the input to filter the possible suggestions. - The
ajaxsetting fetches suggestions with every input change using Ajax requests. - The
lazyClientsetting pre-loads data to the client and uses the input to filter the possible suggestions. The filtering does not start until the input length matches a minimum value. Set the minimum value with theminCharsattribute. - The
cachedAjaxsetting pre-loads data via Ajax requests when the input length matches a minimum value. Set the minimum value with theminCharsattribute. All suggestions are handled on the client until the input prefix is changed, at which point a new request is made based on the new input prefix.
10.1.3. Interactivity options
Users can type into the text field to enter a value, which also searches through the suggestion items in the drop-down box. By default, the first suggestion item is selected as the user types. This behavior can be deactivated by setting
selectFirst="false".
Setting
autofill="true" causes the <rich:autocomplete> to fill the text field box with a matching suggestion as the user types.
To allow users to enter multiple values separated by specific characters, use the
tokens attribute. As the user types, a suggestion will present as normal. When they enter a character specified as a token, this begins a new suggestion process, and the component only uses text entered after the token character for suggestions. For example, if tokens=", " is set, the <rich:autocomplete> component uses both the comma and space characters as tokens to separate entries. When the user enters a comma or a space, a new suggestion process begins.
Note
When declaring tokens, avoid using any characters that are present in the list of suggestions. This may cause unexpected behavior as the user expects the character to match suggestions instead of separating suggested entries.
10.1.4. Customizing the filter in client and lazyClient modes
The
<rich:autocomplete> component uses the JavaScript startsWith() method to create the list of suggestions. The filtering is performed on the client side. Alternatively, use the clientFilterFunction attribute to specify a custom filtering function. The custom function must accept two parameters: the subString parameter is the filtering value as typed into the text box by the user, and the value parameter is an item in the list of suggestions against which the subString must be checked. Each item is iterated through and passed to the function as the value parameter. The custom function must return a boolean value indicating whether the passed item meets the conditions of the filter, and the suggestion list is constructed from successful items.
Example 10.2. Customizing the filter
This example demonstrates how to use a custom filter with the
clientFilterFunction attribute. The custom filter determines if the sub-string is contained anywhere in the suggestion item, instead of just at the start.
<script> function customFilter(subString, value){ if(subString.length>=1) { if(value.indexOf(subString)!=-1) return true; }else return false; }; </script> <h:form> <rich:autocomplete mode="client" minChars="0" autofill="false" clientFilterFunction="customFilter" autocompleteMethod="#{autocompleteBean.autocomplete}" /> </h:form>
10.1.5. JavaScript API
The
<rich:autocomplete> component can be controlled through the JavaScript API. The JavaScript API provides the following functions:
getValue()- Get the current value of the text field.
setValue(newValue)- Set the value of the text field to the
newValuestring passed as a parameter. showPopup()- Show the pop-up list of completion values.
hidePopup()- Hide the pop-up list.
10.1.6. Reference data
component-type:org.richfaces.Autocompletecomponent-class:org.richfaces.component.UIAutocompletecomponent-family:javax.faces.Inputrenderer-type:org.richfaces.AutocompleteRendererhandler-class:org.richfaces.view.facelets.AutocompleteHandler
10.1.7. Style classes and skin parameters
Table 10.1. Style classes (selectors) and corresponding skin parameters
| Class (selector) | Skin Parameters | Mapped CSS properties |
|---|---|---|
| generalTextColor
|
color
|
generalFamilyFont
|
font-family
| |
generalSizeFont
|
font-size
| |
| controlBackgroundColor
|
background-color
|
| panelBorderColor
|
border-color
|
controlBackgroundColor
|
background-color
| |
| No skin parameters. | |
| headerBackgroundColor
|
background-color
|
panelBorderColor
|
border-left-color
| |
| No skin parameters. | |
| No skin parameters. | |
| No skin parameters. | |
| No skin parameters. | |
| headerBackgroundColor
|
background-color
|
generalTextColor
|
border-color
| |
| No skin parameters. | |
| No skin parameters. | |
| No skin parameters. | |
