如何在用户选择下拉列表时触发ajax并更新另一个下拉列表

How to trigger ajax and update another dropdown when user select dropdown

提问人:Neerajkumar 提问时间:5/31/2019 最后编辑:Neerajkumar 更新时间:5/31/2019 访问量:556

问:

我正在实现一个下拉列表更改事件,在更改事件上,另一个下拉列表应根据所选的下拉列表值更新值。

我尝试使用我的实际代码,但没有工作。后来我尝试了Primeface示例下拉列表ajax示例,但也没有用。

Primeface版本:7.0(也尝试了6.1) 爪哇 : 1.8

dropdown.xhtml

<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" action="#{dropdownView.displayLocation}" icon="pi pi-check" />
    </p:panel>
</h:form>```



dropdownView.java

@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 Paulo", "Sao Paulo");
        map.put("Rio de Janerio", "Rio de Janerio");
        map.put("Salvador", "Salvador");
        data.put("Brazil", map);
    }

    public Map<String, Map<String, String>> getData() {
        return data;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

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

    public Map<String, String> getCities() {
        return cities;
    }

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

    public void displayLocation() {
        FacesMessage msg;
        if(city != null && country != null)
            msg = new FacesMessage("Selected", city + " of " + country);
        else
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid", "City is not selected."); 

        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
}```


Expected is on selecting any country, city should load into city dropdown.
Ajax primefaces xhtml

评论

0赞 Raphael_S 5/31/2019
城市的get方法是否在调用了country下拉列表的changeListener之后调用?尝试更新=“:城市”
1赞 Melloware 5/31/2019
此示例工作正常:primefaces.org/showcase/ui/ajax/dropdown.xhtml
0赞 Neerajkumar 5/31/2019
@Melloware,您使用的配置是什么?
0赞 Kukeltje 5/31/2019
来自primefaces展示的那个。其源代码在github中
0赞 Kukeltje 5/31/2019
而“没用”不是一个建设性的评论。发生了什么?检查注释包

答: 暂无答案