Project Documentation
Foundation

Description

This component replicates the 'Value Change Listener' functionality on the client side. It can be used when the user would like a change in the value of one control to trigger off changes in the states of other controls. One or more Javascript Listeners can be nested within the source control (a control belonging to the 'javax.faces.Input' family). When the value of the source control is modified, the listeners are triggered and the states of the target controls modified.

Screen Shot

Not Available

API

component-family javax.faces.Output
renderer-type org.apache.myfaces.JsValueChangeListener
component-class org.apache.myfaces.custom.jslistener.JsValueChangeListener
renderer-class org.apache.myfaces.JsValueChangeListener
tag-class org.apache.myfaces.custom.jslistener.JsValueChangeListenerTag

Usage

                <t:jsValueChangeListener for="id"
                            property="property"
                            expressionValue="{true|false}" 
                            bodyTagEvent="eventName" />
            

Syntax

<t:jsValueChangeListener>

for - the id of the target control
expressionValue - the javascript expression to evaluate. The keyword '$srcElem' resolves to the source control and the keyword '$destElem' resolves to the target control
property(optional) - The result of the evaluated expression is assigned to the specified property of the target control
bodyEventTag(optional) - Events are triggered by the 'onchange' event of the source control. Here, an additional event can be specified (onload?)

Examples

Some examples will throw more light on the usage of this component.

Example 1
Suppose we have two text fields on a page. We would like to keep the value of the second text field in sync with the value of the first. This can be accomplised with the following code:

                <h:inputText id="text1">
                    <t:jsValueChangeListener for="text2" property="value" 
                                                expressionValue="$srcElem.value" />
                </h:inputText>
                <h:inputText id="text2"/>
            

When the value of text1 changes, the 'onchange' event is triggered. The javascript expression specified by 'expressionValue' is evaluated, and the result is assigned to the specified property (in this case, 'value') of the target control.

Example 2
Sometimes, the evalution of the javascript expression itself causes the desired side-effect. In this case, it is not necessary to specify the 'property' attribute for the target control. In this example, we have a combo-box, and we want the selection of a specific value in the combo-box to cause a text box to be hidden.

       <h:selectOneMenu id="selone_menu_colors" value="red" styleClass="selectOneMenu">
           <f:selectItems value="#{carconf.colors}" />
           <t:jsValueChangeListener for="selone_menu_subcolors" 
             expressionValue="($srcElem.options[$srcElem.selectedIndex].value=='black')?
             $destElem.style.display='inline':$destElem.style.display='none';"/>
       </h:selectOneMenu>
       <h:inputText id="selone_menu_subcolors"/>
            

The evaluation of the expression causes the text box to be hidden when the appropriate value is selected.

Instructions

see examples/jslistener.jsp for an example!