根据选定的selectOneMenu项启用jsf组件

时间:2014-01-22 05:49:04

标签: jsf jsf-2 primefaces selectonemenu

我正在使用jsf / primefaces。如果用户从'addCountry'selectOneMenu中选择'Australia',我想启用'addState'selectOneMenu下拉列表,它可以工作但是在第一个实例上你必须在它启用之前更改国家几次,之后它启用/禁用每次更改。第一次更改后如何回复?

    <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>

1 个答案:

答案 0 :(得分:0)

这样怎么样

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;
    }


}

<!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>