-1

I put a valueChangeListener on a primefaces selectOneRadio to render some components on the page according to the selected choice but it doesn't work

<p:selectOneRadio id="Gender"
    value="#{someBean.booleanVariable}"
    required="true"
   valueChangeListener="#{personBean.triggerGender}">
    <f:ajax execute="@this" listener="#{personBean.triggerGender}" render="triggerGender" />
  <f:selectItem itemLabel="Female" itemValue="female" />
  <f:selectItem itemLabel="Male" itemValue="male" /> 
</p:selectOneRadio>

personBean

public class ReportPerson {

private boolean female, male = true;
    public void triggerGender(ValueChangeEvent e) {
    System.err.println("Fired");
    if (e.getNewValue().equals("female")) {
        female = true;
        male = false;
    }
 }

and this is the components in the JSF page that will be rendered according to the values of the booleans female and male

    <h:form id="triggerGender">
                        <h:outputLabel value="Does he has " styleClass="Labels"
                            rendered="#{personBean.male}" />
                        <h:outputLabel rendered="#{personBean.male}" />
                        <h:outputLabel rendered="#{personBean.male}" />

                        <h:outputLabel for="Moustache" value="Mustache ?"
                            styleClass="Labels" rendered="#{personBean.male}" />
                        <p:selectBooleanCheckbox
                            value="#{personBean.personDescription.moustach}"
                            rendered="#{personBean.male}" />
                        <h:outputLabel rendered="#{personBean.male}" />


                        <h:outputLabel value="Beared ? " styleClass="Labels"
                            rendered="#{personBean.male}" />
                        <p:selectBooleanCheckbox
                            value="#{personBean.personDescription.beared}"
                            rendered="#{personBean.male}" />
                        <h:outputLabel rendered="#{personBean.male}" />


                        <h:outputLabel value="Does she wear " styleClass="Labels"
                            rendered="#{personBean.female}" />
                        <h:outputLabel rendered="#{personBean.female}" />
                        <h:outputLabel rendered="#{personBean.female}" />

                        <h:outputLabel value="Describe?" styleClass="Labels"
                            rendered="#{personBean.female}" />
                        <p:selectBooleanCheckbox
                            value="#{personBean.personDescription.hijab}"
                            rendered="#{personBean.female}" />
                        <h:outputLabel rendered="#{personBean.female}" />

                    </h:form>

when I change the radioButton to female the statement Fired is printed in the console but the above component isn't renderedw

Cœur
  • 37,241
  • 25
  • 195
  • 267
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165

2 Answers2

0

Couple of things:

1) You only need to use the valuechange listener if you want need to know the old value and the new value. If you're only rendering components then use:

 <f:ajax execute="@this" listener="#{personBean.triggerGender}" render="triggerGender"></f:ajax>

2) There needs to be a component in your form with the id 'triggerGender' if you want to ajax refresh it

3) Your radio button component is bound to #{someBean.booleanVariable}, however the item values of your select items are strings. Shouldn't they be booleans for true or false?

4) Your outputlabel should be conditionally rendered based on #{someBean.booleanVariable} and should be within a component with the id 'triggerGender'

zargarf
  • 633
  • 6
  • 18
0

You should nested your form inside h:panelGroup and render your PanelGroup