I have the following code in JSF-2 Mojarra 2.0.8 running on Tomcat 7
<h:panelGrid id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
<h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItems value="#{user.choices}"/>
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadio}">
<h:outputLabel>Name :</h:outputLabel>
<h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
<h:outputLabel>Salary: </h:outputLabel>
<h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>
My managedBean contain following
private enum Choice {
Yes, No;
}
private Choice yesNoRadio;
public Choice[] getChoices() {
return Choice.values();
}
public Choice getYesNoRadio() {
return yesNoRadio;
}
public void setYesNoRadio(Choice yesNoRadio) {
this.yesNoRadio = yesNoRadio;
}
How can I render my 'userDetailsGrid' based on the boolean value (user.yesNoRadio) selected in
I found a workarround by adding following in my mangedbean
private Boolean yesNoRadio;
public SelectItem[] getMyBooleanValues() {
return new SelectItem[] {
new SelectItem(Boolean.TRUE, "Yes"),
new SelectItem(Boolean.FALSE, "No")
};
}
and changing my view to
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItems value="#{user.myBooleanValues}" />
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
Use a simple Boolean value in your backing bean:
private Boolean choice;
// getter and setter
And in the facelet:
<h:selectOneRadio id="yesNoRadio" value ="#{user.choice}">
<f:selectItem itemValue="#{false}" itemLabel="No" />
<f:selectItem itemValue="#{true}" itemLabel="Yes"/>
<f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
..
<h:panelGrid id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.choice}">
..
</h:panelGrid>
..
</h:panelGrid>
You can also just check the enum value in the rendered
attribute. Enums are evaluated as strings in EL, so you can do a simple string comparison. This way you can keep your original code.
<h:panelGrid columns="2" rendered="#{user.yesNoRadio == 'Yes'}">
By the way, you perhaps really want to use execute="@this"
instead of execute="@form"
(or just remove it altogether, it defaults to @this
already). The execute="@form"
will process the entire form and thus also fire validation there where you perhaps don't want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With