How can I repopulate my list (stationaryList) ? What’s wrong in my code ? Nothing change in my list if I select value = CDL or MEL or NEF. Thanks.
<h:form id="frmCreateNewStationary">
<ui:param name="s" value="#{myController.selected}"/>
<h:panelGrid columns="2">
<h:outputLabel value="Type" for="idType" />
<h:selectOneMenu id="idType" value="#{s.stationaryid.type}">
<f:selectItem itemLabel="Select ..." noSelectionOption="true" />
<f:selectItem itemLabel="CDL" itemValue="CDL" />
<f:selectItem itemLabel="MEL" itemValue="MEL" />
<f:selectItem itemLabel="NEF" itemValue="NEF" />
<f:ajax event="change" listener="#{myController.changeStationaryCodeList}" render="idCode" execute="@this" />
</h:selectOneMenu>
<h:outputLabel value="Code" for="idCode" />
<h:selectOneMenu id="idCode" value="#{s.stationaryid.code}">
<f:selectItem itemLabel="Select ..." noSelectionOption="true" />
<f:selectItems value="#{myController.stationaryList}" />
</h:selectOneMenu>
</h:panelGrid>
</h:form>
Java
private List<Stationary> stationaryList = null;
public List<Stationary> getStationaryList() {
stationaryList = getCode();
return stationaryList;
}
public void setStationaryList(List<Stationary> stationaryList) {
this.stationaryList = stationaryList;
}
…
public List<Stationary> getCode() {
//"Stationary.ccc", query = "SELECT s.code FROM Stationary s"),
EntityManager emf = facade.getEntityManager();
Query query = emf.createNamedQuery("Stationary.ccc");
return query.getResultList();
}
…
public List<Stationary> getStationaryCodeItems(String type) {
//"Stationary.code", query = "SELECT s.code FROM Stationary s WHERE s.type = :type"),
EntityManager emf = facade.getEntityManager();
Query query = emf.createNamedQuery("Stationary.code");
query.setParameter("type", type);
return query.getResultList();
}
AjaxBehaviorEvent looks like this:
public void changeStationaryCodeList(AjaxBehaviorEvent ev) {
stationaryList.clear();
String type = (String) ((UIInput) ev.getComponent()).getValue();
System.out.println("Test__changeStationaryCodeList -- state == " + type);
stationaryList = getStationaryCodeItems(type);
//setStationaryList(stationaryList);
}
Your
<h:selectOneMenu id="idType">does not have avalueattribute set. Bind it to a value in your backing bean as you already are doing with<h:selectOneMenu id="idCode" value="#{s.stationaryid.code}">and you can then use that attribute in your ajax listener as the submitted value. I also have a feeling you might not be using the right scope here. Annotate your backing bean with a@ViewScopedannotation for hitch-free results in your ajax-ingUnrelated to this, why do you have a backing bean named “s”? That’s just going to make your code painful to read.