junior developper
junior developper

Reputation: 448

Show form when item on selectOneMenu is selected

I am working on j2ee web application (jsf2 and primefaces) and i wonder how to display form depending on the item selected from select one menu. I tried this but it didn't work.

<h:form id="global">
   <p:panel  header="Association">
         ...
     <p:outputLabel  value="Travel Class" />     
     <p:selectOneMenu id="associationType" style="width: 230px" value="#{associationMBean.travelClass}">
                                            <f:selectItem itemLabel="Select one" itemValue="" />  
                                            <f:selectItem itemLabel="Seminar" itemValue="Seminar" />   
                                            <f:selectItem itemLabel="Honeymoon" itemValue="Honeymoon" /> 
                                            <f:selectItem itemLabel="Organized Trip" itemValue="Organized Trip" />
                                            <p:ajax update=":hidePanel"/> 
                                        </p:selectOneMenu>     
   </p:panel>
</h:form>
<p:panel id="hidePanel" style="margin-top: 20px; font-size: 14px; border: 0;display: none " header="Remarks (Optional)"  rendered="#{associationMBean.travelClass eq Seminar}"> 
<h:form>            
<p:inputTextarea style="width: 100% ; height: 100px" value="#{associationMBean.description}" ></p:inputTextarea> 
</h:form>              
</p:panel>

Update

<html 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Visa Application-Thailand</title>

    </h:head>
    <h:body>
        <div align="center">
            <div>
                <img class="logo" src="images/banner.jpg"/>
            </div>
            <p:panel style="width: 61%;  border-color: indianred; border-width: 2px; margin-top: 10px"> 
                <h:form id="global">
                    <p:panel style="margin-top: 20px; font-size: 14px ; border: 0" header="Association">
                        <table border="0" >
                            <tbody > 
                                <tr>
                                    <td align="right">
                                        <p:outputLabel  value="Association Type" />
                                    </td >
                                    <td align="left">
                                        <p:selectOneMenu id="selectmenu" style="width: 230px" value="#{associationMBean.type}">
                                            <f:selectItem itemLabel="Select a Class" itemValue="" />  
                                            <f:selectItem itemLabel="Seminar" itemValue="Seminar" />
                                            <f:selectItem itemLabel="Organized Trip" itemValue="Organized Trip" />
                                            <f:selectItem itemLabel="Honeymoon" itemValue="Honeymoon" />
                                            <p:ajax update=":hidePanel"/>
                                        </p:selectOneMenu>   

                                    </td>
                                </tr>

                                <tr>
                                    <td align="right">
                                        <p:outputLabel value="Date of Departure" for="departure" />
                                    </td>
                                    <td align="left">
                                        <p:calendar  value="#{associationMBean.departure}" showOn="button" pattern="dd/MM/yy" style="width: 250px" id="departure"></p:calendar>
                                        <p:watermark value="01/01/2014" for="departure"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <p:outputLabel value="Date of Arrival" for="arrival" />
                                    </td>
                                    <td align="left">
                                        <p:calendar  value="#{associationMBean.arrival}" showOn="button" pattern="dd/MM/yy" style="width: 250px" id="arrival"></p:calendar>
                                        <p:watermark value="01/01/2014" for="arrival"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <p:outputLabel  value="Travel Agency " for="travelAgency"/>
                                    </td>
                                    <td align="left">
                                        <p:inputText id="travelAgency" value="#{associationMBean.travelAgency}" style="width: 220px"></p:inputText>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <p:outputLabel  value="Airline " for="airline"/>
                                    </td>
                                    <td align="left">
                                        <p:inputText id="airline" value="#{associationMBean.airline}" style="width: 220px"></p:inputText>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <p:outputLabel  value="Travel Class" />
                                    </td >
                                    <td align="left">        
                                        <p:selectOneMenu id="selectclass" style="width: 230px" value="#{associationMBean.travelClass}">
                                            <f:selectItem itemLabel="Select a Class" itemValue="" />  
                                            <f:selectItems value="#{associationMBean.allClasses}" />
                                            <!--  <p:ajax update=":hidePanel"/> !-->
                                        </p:selectOneMenu>                                   
                                    </td>
                                </tr> 

                                <tr >
                                    <td align="right">
                                        <p:outputLabel  value="Hotels " for="hotels" />
                                    </td>
                                    <td align="left">
                                        <p:inputTextarea id="hotels" value="#{associationMBean.hotels}" style="width: 220px"/>
                                    </td>
                                </tr>               
                            </tbody>
                        </table>
                    </p:panel>
                    <div align="right" style="margin-top: 20px ">
                        <p:commandButton type="reset" value="Reset" style="font-size: 14px;"/>
                        <p:commandButton value="Save"  ajax="false" styleClass="ui-priority-primary" style="font-size: 14px;" action="#{associationMBean.addAssociation()}" />
                    </div>
                </h:form>            
                <p:panel id="hidePanel" style="margin-top: 20px; font-size: 14px; border: 0;display: none " header="Remarks (Optional)" >
                    <h:form rendered="#{associationMBean.type == 'Seminar'}" >
                        <p:inputTextarea style="width: 100% ; height: 100px" value="#{visaMBean.description}" ></p:inputTextarea>
                    </h:form>
                </p:panel>         
            </p:panel>
        </div>
    </h:body>
</html>

the code above is the whole html code after adding some updates <h:form rendered="#{associationMBean.type == 'Seminar'}" > But still can't display the hidePane when i select Seminar from the selectOneMenu. Have you any idea what am i missing??

Upvotes: 0

Views: 1700

Answers (1)

user1983983
user1983983

Reputation: 4841

The following should work:

<p:panel id="hidePanel" style="margin-top: 20px; font-size: 14px; border: 0;display: none " header="Remarks (Optional)"> 
  <h:form rendered="#{associationMBean.travelClass eq 'Seminar'}">
    <p:inputTextarea style="width: 100% ; height: 100px" value="#{associationMBean.description}" ></p:inputTextarea> 
  </h:form>              
</p:panel>

You can not update components which are not in the DOM and when a component is not rendered it is not in there. So you need to update the parent of the conditionally rendered component. And as #{associationMBean.travelClass} is a String you need to wrap 'Seminar' with single quotes '.

Upvotes: 1

Related Questions