Today I was developing a cascaded selectOneMenu, for that I followed the Primefaces documentation link
That being the case, I implemented it in my code to make a cascade of these but in 4 levels:
This is the locationBean.java:
package com.nameEmpresa.integrado.bean;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name="ubicaciones")
@ViewScoped
public class UbicacionesBean {
private Map<String, Map<String, String>> data1 = new HashMap<String, Map<String, String>>();
private Map<String, Map<String, String>> data2 = new HashMap<String, Map<String, String>>();
private Map<String, Map<String, String>> data3 = new HashMap<String, Map<String, String>>();
private Map<String, String> paises;
private Map<String, String> departamentos;
private Map<String, String> municipios;
private Map<String, String> barrios;
private String pais;
private String departamento;
private String municipio;
private String barrio;
@PostConstruct
public void inicializar(){
paises = new HashMap<String, String>();
paises.put("Colombia", "Colombia");
paises.put("Peru", "Peru");
Map<String, String> map = new HashMap<String, String>();
map.put("Cundinamarca","Cundinamarca");
map.put("Boyaca","Boyaca");
data1.put("Colombia",map);
map = new HashMap<String, String>();
map.put("depPeru1", "depPeru1");
map.put("depPeru2", "depPeru2");
data1.put("Peru", map);
Map<String, String> map2 = new HashMap<String, String>();
map2.put("Mesitas", "Mesitas");
map2.put("Viota", "Viota");
data2.put("Cundinamarca", map2);
map2 = new HashMap<String, String>();
map2.put("Tunja", "Tunja");
map2.put("Garagoa", "Garagoa");
data2.put("Boyaca", map2);
map2 = new HashMap<String, String>();
map2.put("MunicipioPeru1", "MunicipioPeru1");
map2.put("MunicipioPeru2", "MunicipioPeru2");
data2.put("depPeru1", map2);
map2 = new HashMap<String, String>();
map2.put("MunicipioPeru66", "MunicipioPeru66");
map2.put("MunicipioPeru67", "MunicipioPeru67");
data2.put("depPeru2", map2);
Map<String, String> map3Barr = new HashMap<String, String>();
map3Barr.put("barrio1Mesitas", "barrio1Mesitas");
map3Barr.put("barrio2Mesitas", "barrio2Mesitas");
data3.put("Mesitas", map3Barr);
map3Barr = new HashMap<String, String>();
map3Barr.put("barrioViota1", "barrioViota1");
map3Barr.put("barrioViota2", "barrioViota2");
data3.put("Viota", map3Barr);
map3Barr = new HashMap<String, String>();
map3Barr.put("barrioPeru666", "barrioPeru666");
data3.put("MunicipioPeru67", map3Barr);
}
public Map<String, String> getPaises() {
return paises;
}
public void setPaises(Map<String, String> paises) {
this.paises = paises;
}
public Map<String, String> getDepartamentos() {
return departamentos;
}
public void setDepartamentos(Map<String, String> departamentos) {
this.departamentos = departamentos;
}
public Map<String, String> getMunicipios() {
return municipios;
}
public void setMunicipios(Map<String, String> municipios) {
this.municipios = municipios;
}
public Map<String, String> getBarrios() {
return barrios;
}
public void setBarrios(Map<String, String> barrios) {
this.barrios = barrios;
}
public String getPais() {
return pais;
}
public void setPais(String pais) {
this.pais = pais;
}
public String getDepartamento() {
return departamento;
}
public void setDepartamento(String departamento) {
this.departamento = departamento;
}
public String getMunicipio() {
return municipio;
}
public void setMunicipio(String municipio) {
this.municipio = municipio;
}
public String getBarrio() {
return barrio;
}
public void setBarrio(String barrio) {
this.barrio = barrio;
}
public void onPaisChange(){
if(pais != null && !pais.equals(""))
departamentos = data1.get(pais);
else
departamentos = new HashMap<String,String>();
municipios = new HashMap<String,String>();
barrios = new HashMap<String,String>();
}
public void onDepartamentoChange(){
if(departamento != null && !departamento.equals(""))
municipios = data2.get(departamento);
else
municipios = new HashMap<String,String>();
barrios = new HashMap<String,String>();
}
public void onMunicipioChange(){
if(municipio != null && !municipio.equals(""))
barrios = data3.get(municipio);
else
barrios = new HashMap<String,String>();
}
}
this is the .xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:selectOneMenu id="paises" value="#{ubicaciones.pais}">
<p:ajax listener="#{ubicaciones.onPaisChange()}" update="depar muni barrios"/>
<f:selectItem itemLabel="Seleccione un Pais" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{ubicaciones.paises}" />
</p:selectOneMenu>
<p:selectOneMenu id="depar" value="#{ubicaciones.departamento}" >
<p:ajax listener="#{ubicaciones.onDepartamentoChange()}" update="muni barrios" />
<f:selectItem itemLabel="Seleccione un Departamento" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{ubicaciones.departamentos}" />
</p:selectOneMenu>
<p:selectOneMenu id="muni" value="#{ubicaciones.municipio}">
<p:ajax listener="#{ubicaciones.onMunicipioChange()}" update="barrios"/>
<f:selectItem itemLabel="Seleccione un Municipio" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{ubicaciones.municipios}" />
</p:selectOneMenu>
<p:selectOneMenu id="barrios" value="#{ubicaciones.barrio}">
<f:selectItem itemLabel="Seleccione un Barrio" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{ubicaciones.barrios}" />
</p:selectOneMenu>
</h:form>
</h:body>
</html>
The problem was that the fields were not emptied properly (marked with id's="depar", "muni", "barrios") when the ajax of the countryOnchange was executed. After looking for hours I realized that when I removed the brackets to the method of countryOnChange this worked as I wanted. It was like this:
public void onPaisChange(){
if(pais != null && !pais.equals("")){
departamentos = data1.get(pais);
}else{
departamentos = new HashMap<String,String>();
municipios = new HashMap<String,String>();
barrios = new HashMap<String,String>();
}
}
And then I removed the brackets and it served. staying like this:
public void onPaisChange(){
if(pais != null && !pais.equals(""))
departamentos = data1.get(pais);
else
departamentos = new HashMap<String,String>();
municipios = new HashMap<String,String>();
barrios = new HashMap<String,String>();
}
This is where my question comes from. Is not it supposed that the 2 should be executed in the same way? and Why are those 2 blocks of code not equivalent?
I would really appreciate it if you can resolve this concern I have :).