1

I use 3 selectOneMenu components in my project. I need to reload content of the second after change in the first. Here are some parts of the files

index.xhtml

<h:form id="form">
<p:selectOneMenu id="Rząd" value="#{birdSelectorBean.selectedState}" effect="fade"
style="width: 150px;">  
<f:selectItem itemLabel="Rząd" itemValue="" />  
<f:selectItems value="#{birdSelectorBean.rzad}" />  
<p:ajax render="@form" listener="#{birdSelectorBean.stateChangeListener}" />
</p:selectOneMenu> 

<p:selectOneMenu id="rodzina" value="#{birdSelectorBean.selectedState}" effect="fade"
style="width: 150px;">  
<f:selectItem itemLabel="Rodzina" itemValue="" />
<f:selectItems value="#{birdSelectorBean.rodzina}" />
</p:selectOneMenu>

<p:selectOneMenu id="rodzaj" value="#{birdSelectorBean.selectedState}" effect="fade"
style="width: 150px;">  
<f:selectItem itemLabel="Rodzaj" itemValue="" />   
<f:selectItems value="#{birdSelectorBean.rodzaj}" />
</p:selectOneMenu>
</h:form>

BirdSelectionBean.java:

public class BirdSelectorBean
{
private String selectedState;
private List<SelectItem> rzad;
private List<SelectItem> rodzina;
private List<SelectItem> rodzaj;

public BirdSelectorBean()
{
    rzad = new ArrayList<>();
    rzad.add(new SelectItem("Rząd_X"));
    rzad.add(new SelectItem("Rząd_Y"));
    rzad.add(new SelectItem("Rząd_Z"));

    rodzina = new ArrayList<>();
    rodzaj = new ArrayList<>();
}

public void stateChangeListener(ValueChangeEvent event)
{
    rodzina.clear();
    rodzina.add(new SelectItem("Rodzina_A"));
    rodzina.add(new SelectItem("Rodzina_B"));
    rodzina.add(new SelectItem("Rodzina_C"));
}
...
getters and setters
...
}

I read many topics on that but it doesn't work for me. I tried update="rodzina" like it is in example here and render option like it is said in that topic: But it still doesn't work. Please help me :]

Community
  • 1
  • 1
user2374573
  • 59
  • 1
  • 3
  • 11

2 Answers2

2

In the p:ajax tag change render="@form" to update="@form". Render is used by f:ajax, primefaces use another name for some reason - see here.

dratewka
  • 2,104
  • 14
  • 15
1

Looks like your stateChangeListener method nevers get called and more important, your managed bean looks like hasn't any scope (at least from your question content), remember that it must be at least @ViewScoped in order to make this work. Also, another problem in your code is that you're using the same attribute to select the data for the three <p:selectOnuMenu> (which is not a problem yet since you haven't achieved what you wanted to begin with).

To make the ajax update work, remove the parameter from your stateChangeListener. Also let's add the other two attributes for the selected items from the dropdownlists.

@ManagedBean
@ViewScoped
public class BirdSelectorBean {

    private String selectedState;
    private String selectedStateRodzina;
    private String selectedStateRodzaj;
    //other fields and methods...

    public void stateChangeListener() {
        rodzina.clear();
        rodzina.add(new SelectItem("Rodzina_A"));
        rodzina.add(new SelectItem("Rodzina_B"));
        rodzina.add(new SelectItem("Rodzina_C"));
    }
}

And then update your desired <p:selectOneMenu> in your <p:ajax> call (I removed the non-directly related to the problem attributes from the components like style):

<p:selectOneMenu id="Rząd" value="#{birdSelectorBean.selectedState}">
    <f:selectItem itemLabel="Rząd" itemValue="" />
    <f:selectItems value="#{birdSelectorBean.rzad}" />
    <p:ajax update="rodzina" listener="#{birdSelectorBean.stateChangeListener}" />
</p:selectOneMenu>

<p:selectOneMenu id="rodzina" value="#{birdSelectorBean.selectedStateRodzina}">
    <f:selectItem itemLabel="Rodzina" itemValue="" />
    <f:selectItems value="#{birdSelectorBean.rodzina}" />
</p:selectOneMenu>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thank you very much. Your help was helpful for me. I had bean configured in xml file. But you help me to straighten up my code. – user2374573 Jun 02 '13 at 11:52