0

i'm using jsf/primefaces. I want to enable the 'addState' selectOneMenu dropdown should the user select 'Australia' from the 'addCountry' selectOneMenu, it works but on the first instance you have to change the country a couple of times before it will enable, after that it enables/disables on every change. How to get it to respond after first change?

    <h:outputLabel value="#{bundle.AddressLabel_country}" for="addcountry" />
    <p:selectOneMenu id="addcountry" value="#{addressBean.address.country}" immediate="true" >
        <f:selectItems value="#{localeList.getCountryList()}" />
        <f:ajax render="addstate"/>
    </p:selectOneMenu>

    <h:outputLabel value="#{bundle.AddressLabel_state}" for="addstate"/>
    <p:selectOneMenu id="addstate" value="#{addressBean.address.state}" disabled="#{addressBean.address.country ne 'Australia'}">
        <f:selectItem itemLabel="Select State" itemValue="" /> 
        <f:selectItems value="#{localeList.getAUState()}" />
    </p:selectOneMenu>
Ectomorph
  • 94
  • 10

1 Answers1

0

how about this way

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class AddressBean implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 2439322307625952605L;

    private List<String> countries;
    private List<String> states;
    private Address address;
    private boolean disableState;
    private Map<String,List<String>> map = new HashMap<String,List<String>>();

    @PostConstruct
    public void init(){
        address= new Address();
        countries = new ArrayList<String>();
        states = new ArrayList<String>();

        countries.add("Australia");
        countries.add("Other");

        String[] s1 = {"state 1","state 2"};
        String[] s2 = {"state 3","state 4"};
        map.put("Australia", Arrays.asList(s1));
        map.put("Other", Arrays.asList(s2));

        this.disableState = "Australia".equals(countries.get(0));
    }

    public void updateState(){
        this.states = map.get(this.address.getCountry());
        this.disableState = "Australia".equals(this.address.getCountry());
    }

    public List<String> getCountries() {
        return countries;
    }

    public void setCountries(List<String> countries) {
        this.countries = countries;
    }

    public List<String> getStates() {
        return states;
    }

    public void setStates(List<String> states) {
        this.states = states;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public boolean isDisableState() {
        return disableState;
    }

    public void setDisableState(boolean disableState) {
        this.disableState = disableState;
    }


}

and

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
        <h:form>
            <h:outputLabel value="Country"
                for="addcountry" />
            <p:selectOneMenu id="addcountry" value="#{addressBean.address.country}">
                <f:selectItems value="#{addressBean.getCountries()}" />
                <p:ajax listener="#{addressBean.updateState}" update="addstate"/>
            </p:selectOneMenu>

            <h:outputLabel value="State" for="addstate" />
            <p:selectOneMenu id="addstate" value="#{addressBean.address.state}" disabled="#{addressBean.disableState}">
                <f:selectItem itemLabel="Select State" itemValue="" />
                <f:selectItems value="#{addressBean.getStates()}" />
            </p:selectOneMenu>
        </h:form>
</h:body>
</html>
  • Good approach but not quite right for my how my code is structured unfortunately. The actual problem seems to be that since the addressBean.adress.country is an empty string initially the event doesn't get fired until it has been assigned a value which is then subsequently changed, meaning the event is not fired until users' second selection. – Ectomorph Jan 24 '14 at 04:00
  • Update : I removed the 'disabled' from the second seleconemenu so it is always rendered, then when testing i also made a selection from this dropdown, if the ajax update was being called it would reset this selection, which it does. So, to sum up, the ajax render'addState' is being called each time, only the value of address.country isn't being updated until the second selection. – Ectomorph Jan 24 '14 at 05:01