on-keypress suggestion rich:suggestionbox HtmlsuggestionBox

Component identification parameters

Name | Value
--- | ---
component-type | org.richfaces.SuggestionBox
component-class | org.richfaces.component.html.HtmlSuggestionBox
component-family | org.richfaces.SuggestionBox
renderer-type | org.richfaces.SuggestionBoxRenderer
tag-class | org.richfaces.taglib.SuggestionBoxTag

## Creating the Component with a Page Tag

To create the simplest variant on a page use the following syntax:

Example:

    ...
        <h:inputText value="#{bean.property}" id="suggest"/>
        <rich:suggestionbox for="suggest" suggestionAction="#{bean.autocomplete}"  var="suggest">
            <h:column>
                <h:outputText value="#{suggest.text}"/>
            </h:column>
        </rich:suggestionbox>
    ...

Here is the `bean.autocomplete` method that returns the collection to pop up:

Example:

    public List autocomplete(Object event) {
        String pref = event.toString();
        //collecting some data that begins with "pref" letters.
            ...
            return result;
        }

## Creating the Component Dynamically Using Java

Example:

    import org.richfaces.component.html.HtmlSuggestionBox;
    ...
    HtmlSuggestionBox myList = new HtmlSuggestionBox();
    ...

## Details of Usage

As it is shown in the example above, the main component attribute are:

- "for"

  The attribute where there is an input component which activation causes a suggestion activation

- "suggestionAction"

  is an accepting parameter of a suggestionEvent type that returns as a result a collection for rendering in a tool tip window.

- "var"

  a collection name that provides access for inputting into a table in a popup

There are also two size attributes ( "width" and "height" ) that are obligatory for the suggestion component. The attributes have initial Defaults but should be specified manually in order to be changed.

The suggestionbox component, as it is shown on the screenshot, could get any collection for an output and outputs it in a ToolTip window the same as a custom dataTable (in several columns)

    ...
        <rich:suggestionbox for="test" suggestionAction="#{bean.autocomplete}" var="cit" fetchValue="#{cit.text}">
            <h:column>
                <h:outputText value="#{cit.label}"/>
            </h:column>
            <h:column>
                <h:outputText value="#{cit.text}"/>
            </h:column>
        </rich:suggestionbox>
    ...

It looks on a page in the following way:

<rich:suggestionbox> with ToolTip window

When some string is chosen input receives the corresponding value from the second column containing `#{cit.text}`

There is also one more important attribute named "tokens" that specifies separators after which a set of some characters sequence is defined as a new prefix beginning from this separator and not from the string beginning.

Example:

    ...
        <rich:suggestionbox for="test" suggestionAction="#{bean.autocomplete}" var="cit" selfRendered="true" tokens=",">
            <h:column>
                <h:outputText value="#{cit.text}"/>
            </h:column>
        </rich:suggestionbox>
    ...

This example shows that when a city is chosen and a comma and first letter character are input, Ajax request is called again, but it submits a value starting from the last token:

<rich:suggestionbox> with chosen word

For a multiple definition use either "`,.;` " syntax as a value for tokens or link a parameter to some bean property transmitting separators collection.

The component also encompasses "style" attributes corresponding to dataTable ones for a table appearing in popup (for additional information, read JSF Reference) and custom attribute managing AJAX requests sending (for additional information, see Ajax4JSF Project).

In addition to these attributes common for Ajax action components and limiting requests quantity and frequency, suggestionbox has one more its own attribute limiting requests (the "minChars" attribute). The attribute defines characters quantity inputted into a field after which Ajax requests are called to perform suggestion.

There is possibility to define what be shown if the autocomplete returns empty list. Attribute "nothingLabel" or facet with the same name could be used for it.

Example:

    ...
        <rich:suggestionbox nothingLabel="Empty" for="test" suggestionAction="#{bean.autocomplete}" var="cit">
            <h:column>
                <h:outputText value="#{cit.text}"/>
            </h:column>
        </rich:suggestionbox>
    ...

Example:

    ...
    <rich:suggestionbox for="test" suggestionAction="#{bean.autocomplete}" var="cit">
    	<f:facet name="nothingLabel">
    		<h:outputText value="Empty"/>
    	</f:facet>
    	<h:column>
    		<h:outputText value="#{cit.text}"/>
    	</h:column>
    </rich:suggestionbox>
    ...

It looks on a page in the following way:

<rich:suggestionbox> with empty list

There is such feature in <rich:suggestionbox> component as object selection. If you want that selected item has been represented as object, you could set to "true" the value for "usingSuggestObjects" attribute, "false" value means that selected item represents as string.

Example:

    ...
    <rich:suggestionbox for="test" suggestionAction="#{bean.autocomplete}" var="cit" usingSuggestObjects="true">
            <h:column>
                    <h:outputText value="#{cit.text}"/>
            </h:column>
    </rich:suggestionbox>
    ...

Information about the "process" attribute usage you can findin the "Decide what to process" guide section.

In RichFaces Wiki article about Additional Properties you can find example of getting additional properties.

## JavaScript API

JavaScript API

Function | Description
--- | ---
callSuggestion() | Calls the suggestion. If the "ignoreMinChars" value is "true" then the number of symbols to send a query is no longer actual for callSuggestion()
getSelectedItems() | Returns the array of objects

## Facets

Facets

Facet name | Description
--- | ---
nothingLabel | Redefines the content item if the autocomplete returns empty list. Related attribute is "nothingLabel"
popup | Redefines the content for the popup list of the suggestion
header | Defines the header content
footer | Defines the footer content

## Look-and-Feel Customization

For skinnability implementation, the components use a style class redefinition method. Default style classes are mapped on skin parameters.

There are two ways to redefine the appearance of all <rich:suggestionbox> components at once:

- Redefine the corresponding skin parameters

- Add to your style sheets style classes used by a <rich:suggestionbox> component

## Skin Parameters Redefinition

General skin parameters redefinition for popup list

Parameters for popup list | CSS properties
--- | ---
additionalBackgroundColor | background-color
panelBorderColor | border-color

Skin parameters redefinition for shadow element of the list

Parameters for shadow element of the list | CSS properties
--- | ---
shadowBackgroundColor | background-color
shadowBackgroundColor | border-color
shadowOpacity | opacity

Skin parameters redefinition for popup table rows

Parameters for popup table rows | CSS properties
--- | ---
generalSizeFont | font-size
generalTextColor | color
generalFamilyFont | font-family

Skin parameters redefinition for selected row

Parameters for selected row | CSS properties
--- | ---
headerBackgroundColor | background-color
generalSizeFont | font-size
generalFamilyFont | font-family
headerTextColor | color

## Definition of Custom Style Classes

Classes names

On the screenshot, there are classes names defining specified elements.

Classes names that define a suggestionbox

Class name | Description
--- | ---
rich-sb-common-container | Defines styles for a wrapper <div> element of a suggestion container
rich-sb-ext-decor-1 | Defines styles for the first wrapper <div> element of a suggestion box exterior
rich-sb-ext-decor-2 | Defines styles for the second wrapper <div> element of a suggestion box exterior
rich-sb-ext-decor-3 | Defines styles for the third wrapper <div> element of a suggestion box exterior
rich-sb-overflow | Defines styles for a wrapper <div> element
rich-sb-int-decor-table | Defines styles for a suggestion box table
rich-sb-int | Defines the styles for a suggestion box table rows (tr)
rich-sb-cell-padding | Defines the styles for suggestion box table cells (td)
rich-sb-int-sel | Defines styles for a selected row
rich-sb-shadow | Defines styles for a suggestion boxshadow

In order to redefine styles for all <rich:suggestionbox> components on a page using CSS, it's enough to create classes with the same names (possible classes could be found in the tables above) and define necessary properties in them.

Example:

    ...
    .rich-sb-int{
            font-weight:bold;
    }
    ...

This is a result:

Redefinition styles with predefined classes

In the example the font weight for rows was changed.

Also it's possible to change styles of particular <rich:suggestionbox> component. In this case you should create own style classes and use them in corresponding <rich:suggestionbox> styleClass attributes. An example is placed below:

Example:

    ...
    .myClass{
            background-color:#f0ddcd;
    }
    ...

The "selectedClass" attribute for <rich:suggestionbox> is defined as it's shown in the example below:

Example:

    <rich:suggestionbox ... selectedClass="myClass"/>

This is a result:

Redefinition styles with own classes and styleClass attributes

As it could be seen on the picture above,background color for selected item was changed.

## Relevant Resources Links

Vizit SuggestionBox page at RichFaces Livedemo for examples of component usage and sources.

RichFaces cookbook at JBoss Portal includes some articles that cover different aspects of working with <rich:suggestionbox>:

- "Creating suggestion box dynamically";

- "Getting additional properties from <rich:suggectionbox>".