Dependent h:selectOneMenu problem
jmiguel77 May 3, 2007 5:43 PMHi
I am trying to make two interdependent selectOneMenu objects, but i must be missing something because in every example i see, there is not much code involved but i can't get it to work
this is the code in the page:
<h:form id="agenciaForm">
<a4j:outputPanel ajaxRendered="true">
<h:messages errorClass="errorMessageStyle" infoClass="infoMessageStyle" showDetail="true" showSummary="false" layout="table"/>
</a4j:outputPanel>
<h:panelGrid columns="2">
<h:outputLabel value="Provincia:" styleClass="normal"/>
<h:selectOneMenu id="provincia" value="#{agenciaManager.provincia}" title="Provincia" styleClass="normal">
<f:selectItem itemValue="" itemLabel="Seleccione" />
<f:selectItems value="#{agenciaManager.provincias}"/>
<a4j:support id="provinciaSupport" event="onchange" reRender="canton" immediate="true"/>
</h:selectOneMenu>
<h:outputLabel value="Ciudad:" styleClass="normal"/>
<h:selectOneMenu id="canton" value="#{agenciaManager.canton}" title="Canton" styleClass="normal">
<f:selectItem itemValue="" itemLabel="Seleccione" />
<f:selectItems value="#{agenciaManager.cantones}"/>
</h:selectOneMenu>
</h:panelGrid>
</h:form>
The a4j:support is immediate="true" because in the form, i have some required objects
This is the backingBean code:
private String provincia;
private String canton;
public SelectItem[] getProvincias(){
List<Provincia> provincias = provinciaServiceDelegate.findAllSort();
SelectItem items[] = new SelectItem[provincias.size()];
int i = 0;
for (Provincia provincia : provincias) {
items = new SelectItem(provincia.getProvinciaPK().getProvincia(), provincia.getDescripcion());
i++;
}
return items;
}
public SelectItem[] getCantones() {
System.out.println("getCantones >>> provincia >>> " + provincia);
SelectItem items[] = new SelectItem[0];
if (provincia != null && !provincia.equals("")) {
ProvinciaPK pk = new ProvinciaPK();
pk.setPais("01");
pk.setProvincia(provincia);
List<Canton> cantones = cantonServiceDelegate.findByProvincia(pk);
items = new SelectItem[cantones.size()];
int i = 0;
for(Canton canton : cantones) {
items = new SelectItem(canton.getArcccanPK().getCanton(), canton.getDescripcion());
i++;
}
}
return items;
}
the thing is that i want the canton h:selectOneMenu to be refreshed when i select a provincia h:selectOneMenu
but the problem is that when the getCantones method is triggered the provincia value is always null, so i dont find any related cantones (its a parent-child relation)
what am i missing ??
is there any complete example that i can follow ??
thanks a lot