通过两个<p:selectonemenu> </p:selectonemenu> </p:commandbutton>启用/禁用<p:commandbutton>

时间:2013-10-18 19:54:58

标签: java jsf primefaces selectonemenu commandbutton

当两个必需的selectOneMenu组件没有空值时,我需要启用commandButton

这是我的观点:

<p:selectOneMenu converter="omnifaces.SelectItemsConverter" id="countries" required="true" value="#{countryBean.selectedCountry}">
    <f:selectItem itemLabel="#{msg['choose.option']}" />
    <f:selectItems value="#{countryBean.countries}" var="country" itemLabel="#{country.name}" />
    <f:ajax listener="#{countryBean.changeCountry}" render="cities" update="findPeopleButton" />
</p:selectOneMenu>
<p:selectOneMenu converter="omnifaces.SelectItemsConverter" id="cities" required="true" value="#{countryBean.city}">
    <f:selectItem itemLabel="#{msg['choose.option']}" />
    <f:selectItems value="#{countryBean.cities}" var="city" itemLabel="#{city.name}" />
    <f:ajax listener="#{countryBean.verifyRequiredFields}" update="findPeopleButton" />
</p:selectOneMenu>
<p:commandButton icon="ui-icon-search"
    disabled="#{countryBean.disablePeopleButton}"
    id="findPeopleButton"
    onclick="peopleDialog.show()" />

我在视图加载时禁用了按钮,然后当我为每个selectOneMenu选择值时按钮启用但在此之后当我设置一些selectOneMenu的空值时,我得到一个必需的验证错误消息并且按钮保持启用

这是我的支持bean:

private Country country; // +getter+setter
private City city; // +getter+setter
private List<Country> countries; // +getter
private List<City> cities; // +getter
private boolean disablePeopleButton; // +getter+setter

@EJB
private CountryService countryService;

@PostConstruct
public void init() {
    disablePeopleButton = true;
    countries = countryService.getCountries();
}

public void changeCountry() {
    cities = countryService.getCities(country);
}

public void verifyRequiredFields() {
    if (country != null && city != null) {
        disablePeopleButton = false;
    } else {
        disablePeopleButton = true;
    }
}

我该怎么做才能解决它?

2 个答案:

答案 0 :(得分:0)

试试这个:

<h:form>
    <p:selectOneMenu id="countries"
        converter="omnifaces.SelectItemsConverter"
        value="#{countryBean.selectedCountry}">
        <f:selectItem itemLabel="#{msg['choose.option']}"/>
        <f:selectItems value="#{countryBean.countries}" var="country"
            itemLabel="#{country.name}" />
        <p:ajax listener="#{countryBean.changeCountry()}" 
                update="cities findPeopleButton" />
    </p:selectOneMenu>
    <p:selectOneMenu id="cities"
        converter="omnifaces.SelectItemsConverter"
        value="#{countryBean.city}">
        <f:selectItem itemLabel="#{msg['choose.option']}" />
        <f:selectItems value="#{countryBean.cities}" var="city"
            itemLabel="#{city.name}" />
        <p:ajax update="findPeopleButton" />
    </p:selectOneMenu>
    <p:commandButton icon="ui-icon-search"
        disabled="#{empty countryBean.city}"
        id="findPeopleButton" onclick="peopleDialog.show()" />
</h:form>

和此:

    private Country selectedCountry; // +getter+setter
    private City city; // +getter+setter
    private List<Country> countries; // +getter
    private List<City> cities; // +getter

    @EJB
    private CountryService countryService;

    @PostConstruct
    public void init() {
        countries = countryService.getCountries();
    }

    public void changeCountry() {
        if (selectedCountry != null) {
            cities = countryService.getCities(selectedCountry);
        }
        city = null;
    }

不知道这与您的问题有何关联,但您在Facelets代码中使用#{countryBean.selectedCountry},而在bean中只有country。您还在render上使用了只有前者的update<f:ajax>,而<p:ajax>只有后者。

答案 1 :(得分:0)

itemValue=""

中使用p:selectOneMenu
<p:selectOneMenu converter="omnifaces.SelectItemsConverter" id="countries" required="true" value="#{countryBean.selectedCountry}">
    <f:selectItem itemLabel="#{msg['choose.option']}" itemValue=""/>
    <f:selectItems value="#{countryBean.countries}" var="country" itemLabel="#{country.name}" />
    <f:ajax listener="#{countryBean.changeCountry}" render="cities" update="findPeopleButton" />
</p:selectOneMenu>
<p:selectOneMenu converter="omnifaces.SelectItemsConverter" id="cities" required="true" value="#{countryBean.city}">
    <f:selectItem itemLabel="#{msg['choose.option']}" itemValue=""/>
    <f:selectItems value="#{countryBean.cities}" var="city" itemLabel="#{city.name}" />
    <f:ajax listener="#{countryBean.verifyRequiredFields}" update="findPeopleButton" />
</p:selectOneMenu>
<p:commandButton icon="ui-icon-search"
    disabled="#{countryBean.disablePeopleButton}"
    id="findPeopleButton"
    onclick="peopleDialog.show()" />