0

Refer to this primefaces dropdown link here https://www.primefaces.org/showcase/ui/ajax/dropdown.xhtml and I have used it to build a country and state relationship successfully. I have just been asked to add a city as another dependency on state and I wonder how I could build up the hashmap for the city?

Anyone could offer me some help? I am very new to primefaces, jsf and hibernator and all that and so many thanks if you could help me to take a look at this.

Primefaces 3.4.2, JSF2.1.29

public void onCountryChange() {
    if(country !=null && !country.equals(""))
        cities = data.get(country);
    else
        cities = new HashMap<String, String>();
}


<h:form>
<p:growl id="msgs" showDetail="true" />

<p:panel header="Select a Location" style="margin-bottom:10px;">
    <h:panelGrid columns="2" cellpadding="5">
        <p:outputLabel for="country" value="Country: " />
        <p:selectOneMenu id="country" value="#{dropdownView.country}" style="width:150px">
            <p:ajax listener="#{dropdownView.onCountryChange}" update="city" />
            <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{dropdownView.countries}" />
        </p:selectOneMenu>

        <p:outputLabel for="city" value="City: " />
        <p:selectOneMenu id="city" value="#{dropdownView.city}" style="width:150px">
            <f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{dropdownView.cities}" />
        </p:selectOneMenu>
    </h:panelGrid>

    <p:separator />

    <p:commandButton value="Submit" update="msgs" actionListener="#{dropdownView.displayLocation}" icon="ui-icon-check" />
</p:panel>

@ManagedBean
@ViewScoped
public class DropdownView implements Serializable {

private Map<String,Map<String,String>> data = new HashMap<String, Map<String,String>>();
private String country; 
private String city;  
private Map<String,String> countries;
private Map<String,String> cities;

@PostConstruct
public void init() {
    countries  = new HashMap<String, String>();
    countries.put("USA", "USA");
    countries.put("Germany", "Germany");
    countries.put("Brazil", "Brazil");

    Map<String,String> map = new HashMap<String, String>();
    map.put("New York", "New York");
    map.put("San Francisco", "San Francisco");
    map.put("Denver", "Denver");
    data.put("USA", map);

    map = new HashMap<String, String>();
    map.put("Berlin", "Berlin");
    map.put("Munich", "Munich");
    map.put("Frankfurt", "Frankfurt");
    data.put("Germany", map);

    map = new HashMap<String, String>();
    map.put("Sao Paolo", "Sao Paolo");
    map.put("Rio de Janerio", "Rio de Janerio");
    map.put("Salvador", "Salvador");
    data.put("Brazil", map);
}
1
  • The showcase you refer to is for 6.2 and your mentioned version is 3.4... keep that in mind
    – Kukeltje
    Sep 9, 2018 at 6:55

1 Answer 1

0

I think I got it to work:

    countries1  = new HashMap<String, String>();
    countries1.put("USA", "USA");
    countries1.put("Malaysia", "Malaysia");

    Map<String,String> map = new HashMap<String, String>();
    map.put("Washington", "Washington");
    map.put("California", "California");
    data.put("USA", map);

    map = new HashMap<String, String>();
    map.put("Johor", "Johor");
    map.put("Penang", "Penang");
    data.put("Malaysia", map);

    Map<String,String> map1 = new HashMap<String, String>();
    map1.put("Washington City1", "Washington City1");
    map1.put("Washington City2", "Washington City2");
    data1.put("Washington", map1);

    map1 = new HashMap<String, String>();
    map1.put("California City1", "California City1");
    map1.put("California City2", "California City2");
    data1.put("California", map1);

    map1 = new HashMap<String, String>();
    map1.put("Johor City1", "Johor City1");
    map1.put("Johor City2", "Johor City2");
    data1.put("Johor", map1);

    map1 = new HashMap<String, String>();
    map1.put("Penang City1", "Penang City1");
    map1.put("Penang City2", "Penang City2");
    data1.put("Penang", map1);

-

<h:outputLabel for="country_id1" value="#{msg.countryLabel}: *" />
                <p:selectOneMenu id="country_id1" value="#{userBean.country1}"
                    style="width:150px">
                    <p:ajax listener="#{userBean.onCountryChange}" update="state2" />
                    <f:selectItem itemLabel="Select Country" itemValue=""
                        noSelectionOption="false" />
                    <f:selectItems value="#{userBean.countries1}" />
                </p:selectOneMenu>
                <p:message for="country_id1" />

                <h:outputLabel for="state2" value="State: *" />
                <p:selectOneMenu id="state2" value="#{userBean.state}"
                    style="width:150px">
                    <p:ajax listener="#{userBean.onStateChange}" update="city22" />
                    <f:selectItem itemLabel="Select State" itemValue=""
                        noSelectionOption="false" />
                    <f:selectItems value="#{userBean.states}" />
                </p:selectOneMenu>
                <p:message for="state2" />

                <h:outputLabel for="city22" value="City: *" />
                <p:selectOneMenu id="city22" value="#{userBean.city1}"
                    style="width:150px">
                    <f:selectItem itemLabel="Select City" itemValue=""
                        noSelectionOption="true" />
                    <f:selectItems value="#{userBean.cities1}" />
                </p:selectOneMenu>

public void onCountryChange() {
    lg.info("country in change: " + country1);

    if (country1 != null && !country1.equals("")) {
        states = data.get(country1);
    }
    else
        states = new HashMap<String, String>();
}

public void onStateChange() {
    lg.info("state in change: " + state);

    if (state != null && !state.equals("")) {
        cities1 = data1.get(state);
    }
    else
        cities1 = new HashMap<String, String>();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.