0

我正在使用两个 selectOneMenu。选择第一个应该通过提供所选值作为参数来填充第二个。我在网上查了很多东西,但仍然没有找到解决方法。侦听器方法在下拉值更改时被调用。有人请帮帮我。(仅当我启用注释行时才有效)

<p:selectOneMenu value="#{myBean.mGroup}" id="mGroup" style="width:130px;" >
     <f:selectItem itemLabel="Environment" itemValue="E"></f:selectItem>
    <f:selectItem itemLabel="Health" itemValue="H"></f:selectItem>
    <f:selectItem itemLabel="Physical" itemValue="P"></f:selectItem>
    <p:ajax render="@form" listener="#{myBean.mGroupChangedListener}" />
</p:selectOneMenu> 

<p:selectOneMenu id="mClass" style="width: 130px;" value="#{myBean.mClass}">
    <!--  <f:selectItem itemLabel="Temp1" itemValue="Temp1" />      if i remove comment from this line, it works         -->
    <f:selectItems value="#{myBean.mClassList}" var="clas" itemLabel="#{clas.mClassDesc}" itemValue="#{clas.mClassId}" />
</p:selectOneMenu>

// listener method
public void mGroupChangedListener(AjaxBehaviorEvent event) {
    List<MClass>  mClassList = service.getMClass(event.getComponent().getAttributes().get("value").toString());
    myBean.setMClassList(mClassList);
}
4

2 回答 2

1

替换<p:ajax render="@form"<p:ajax update="@form"

在 primefaces ( p:ajax) 中使用:

update代替render

process代替execute

于 2013-09-12T06:55:22.187 回答
1

我意识到这是一篇旧帖子,但也许我的回答可能对某人有所帮助。这是一个填充第二个下拉列表的工作示例:

这是支持 bean:

    package com.test.beans;

    import java.util.ArrayList;
    import java.util.List;

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    import javax.faces.component.UIInput;
    import javax.faces.event.AjaxBehaviorEvent;
    import javax.faces.model.SelectItem;

    @ManagedBean(name = "countryBean")
    @RequestScoped
    public class CountryBean {

        private List<SelectItem> country;
        private List<SelectItem> region;

        public CountryBean() {
            country = new ArrayList<SelectItem>();
            region = new ArrayList<SelectItem>();
        }

        public void changeRegion(AjaxBehaviorEvent ev) {

            // clear the drop-down
            region.clear();

            String value = "";

            if (ev == null) {
                System.out.println("Ajax event is null!");
            } else {
                UIInput input = (UIInput) ev.getSource();
                value = (String) input.getValue();
            }

            if (value.equals("Canada")) {

                System.out.println("CND value!");

                region.add(new SelectItem("Alberta"));
                region.add(new SelectItem("British Columbia"));
                region.add(new SelectItem("Manitoba"));
                region.add(new SelectItem("New Brunswick"));
                region.add(new SelectItem("Newfoundland & Labrador"));
                region.add(new SelectItem("Northwest Territories"));
                region.add(new SelectItem("Nova Scotia"));
                region.add(new SelectItem("Nunavut"));
                region.add(new SelectItem("Ontario"));
                region.add(new SelectItem("Quebec"));
                region.add(new SelectItem("Prince Edward Island"));
                region.add(new SelectItem("Saskatchewan"));
                region.add(new SelectItem("Yukon"));

            } else if (value.equals("USA")) {

                System.out.println("USA value!");

                region.add(new SelectItem("Alabama"));
                region.add(new SelectItem("Alaska"));
                region.add(new SelectItem("Arizona"));
                region.add(new SelectItem("Arkansas"));
                region.add(new SelectItem("California"));
                region.add(new SelectItem("Colorado"));
                region.add(new SelectItem("Connecticut"));
                region.add(new SelectItem("Delaware"));
                region.add(new SelectItem("Florida"));
                region.add(new SelectItem("Georgia"));
                region.add(new SelectItem("Hawaii"));
                region.add(new SelectItem("Idaho"));
                region.add(new SelectItem("Illinois"));
                region.add(new SelectItem("Indiana"));
                region.add(new SelectItem("Iowa"));
                region.add(new SelectItem("Kansas"));
                region.add(new SelectItem("Kentucky"));
                region.add(new SelectItem("Louisiana"));
                region.add(new SelectItem("Maine"));
                region.add(new SelectItem("Maryland"));
                region.add(new SelectItem("Massachusetts"));
                region.add(new SelectItem("Michigan"));
                region.add(new SelectItem("Minnesota"));
                region.add(new SelectItem("Mississippi"));
                region.add(new SelectItem("Missouri"));
                region.add(new SelectItem("Montana"));
                region.add(new SelectItem("Nebraska"));
                region.add(new SelectItem("Nevada"));
                region.add(new SelectItem("New Hampshire"));
                region.add(new SelectItem("New Jersey"));
                region.add(new SelectItem("New Mexico"));
                region.add(new SelectItem("New York"));
                region.add(new SelectItem("North Carolina"));
                region.add(new SelectItem("North Dakota"));
                region.add(new SelectItem("Ohio"));
                region.add(new SelectItem("Oklahoma"));
                region.add(new SelectItem("Oregon"));
                region.add(new SelectItem("Pennsylvania"));
                region.add(new SelectItem("Rhode Island"));
                region.add(new SelectItem("South Carolina"));
                region.add(new SelectItem("South Dakota"));
                region.add(new SelectItem("Tennessee"));
                region.add(new SelectItem("Texas"));
                region.add(new SelectItem("Utah"));
                region.add(new SelectItem("Vermont"));
                region.add(new SelectItem("Virginia"));
                region.add(new SelectItem("Washington"));
                region.add(new SelectItem("West Virginia"));
                region.add(new SelectItem("Wisconsin"));
                region.add(new SelectItem("Wyoming"));

            }

        }

        // getters and setters

        public List<SelectItem> getCountry() {
            return country;
        }

        public void setCountry(List<SelectItem> country) {
            this.country = country;
        }

        public List<SelectItem> getRegion() {
            return region;
        }

        public void setRegion(List<SelectItem> region) {
            this.region = region;
        }

    }

这是形式:

    <!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://xmlns.jcp.org/jsf/facelets"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:f="http://xmlns.jcp.org/jsf/core">

    <h:head>
        <title>Form</title>
    </h:head>
    <h:body>

        <h:panelGrid columns="2">
            <h:form>

                <!-- country -->
                <h:outputText value="Select a country: "/>

                <h:selectOneMenu id="countryDropDown">
                    <f:selectItem itemLabel="Canada" itemValue="Canada" />
                    <f:selectItem itemLabel="USA" itemValue="USA" />
                        <f:ajax listener="#{countryBean.changeRegion}" render="regionDropDown" />
                </h:selectOneMenu>

                <!-- region -->
                <h:outputText value="Select a region: "/>

                <h:selectOneMenu id="regionDropDown">
                    <f:selectItems value="#{countryBean.region}" />
                </h:selectOneMenu>
            </h:form>

        </h:panelGrid>

    </h:body>
    </html>
于 2017-03-24T00:58:39.917 回答