java SelectOneMenu 更新其他 SelectOneMenu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12533945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:17:11  来源:igfitidea点击:

SelectOneMenu updates other SelectOneMenu

javaajaxjsfmanaged-beanselectonemenu

提问by thomas.st

I want to update the second SelectOneMenu when I select any item of the first SelectOnMenu. As it is now, I get the values for the SelectOneMenus from a ManagedBean. I guess I've to use AJAX (jquery) to send parameters to the ManagedBean.

当我选择第一个 SelectOnMenu 的任何项目时,我想更新第二个 SelectOneMenu。现在,我从 ManagedBean 获取 SelectOneMenus 的值。我想我必须使用 AJAX (jquery) 将参数发送到 ManagedBean。

<h:form>
    <div class="center">
        <h:panelGrid id="editTable" columns="2" styleClass="center">
            ...
            <h:outputText value="#{msg.timetable_list_category}" />
            <h:selectOneMenu class="category">
                <f:selectItems value="#{categoryBackingBean.categorys}" var="c"
                    itemLabel="#{c.category_Name}" itemValue="#{c.id}" />
            </h:selectOneMenu>

                <h:outputText value="#{msg.timetable_list_seminarblock}" />
            <h:selectOneMenu class="seminarblock">
                <f:selectItems value="#{seminarblockBackingBean.seminarblocks}" var="s"
                    itemLabel="#{s.seminarblock_Name}" itemValue="#{s.seminarblock_Id}" />
            </h:selectOneMenu>
            ...
        </h:panelGrid>
        ...
    </div>
</h:form>

回答by Fritz

Actually you can use a ValueChangeListener that is invoked when the value of your selectOneMenu changes:

实际上,您可以使用在 selectOneMenu 的值更改时调用的 ValueChangeListener :

<h:selectOneMenu class="category" valueChangeListener="#{yourBean.selectOneMenuListener}">
    <f:selectItems value="#{categoryBackingBean.categorys}" var="c"
        itemLabel="#{c.category_Name}" itemValue="#{c.id}" />
</h:selectOneMenu>

Then, in your bean you have this method:

然后,在你的 bean 中你有这个方法:

public void selectOneMenuListener(ValueChangeEvent event) {
    //This will return you the newly selected
    //value as an object. You'll have to cast it.
    Object newValue = event.getNewValue(); 
    //The rest of your processing logic goes here...
}

To update the page you can either add onchange="submit()"to your <h:selectOneMenu/>. For some partial rendering you can try adding this <f:ajax/>instead of onchange="submit()":

要更新页面,您可以添加onchange="submit()"到您的<h:selectOneMenu/>. 对于某些部分渲染,您可以尝试添加它<f:ajax/>而不是onchange="submit()"

<h:selectOneMenu class="category" valueChangeListener="#{yourBean.selectOneMenuListener}">
    <f:selectItems value="#{categoryBackingBean.categorys}" var="c"
        itemLabel="#{c.category_Name}" itemValue="#{c.id}" />
    <f:ajax event="change" execute="@form" render="theIdOfTheComponentYouWantToReRender"/>
</h:selectOneMenu>

If I'm not mistaken you'll want to get the id of the element selected in the first menu and populate the second one according to it. Then you can render the other selectOneMenu or, if needed, a panel wrapping a part of your form.

如果我没记错的话,您会想要获取在第一个菜单中选择的元素的 id 并根据它填充第二个。然后,您可以呈现另一个 selectOneMenu,或者,如果需要,可以呈现一个包含表单一部分的面板。

回答by Paulius Matulionis

Primefaceshas a great feature of what you trying to achieve. It already using Ajax, so don't have to worry about writing code your self.

Primefaces有一个很棒的功能,可以帮助您实现目标。它已经在使用 Ajax,所以不必担心自己编写代码。