如何使用selectOneListBox和AJAX动态更改panelGroup

时间:2013-01-10 14:21:05

标签: ajax jsf

我想根据所选项目更改菜单的外观。

如何使用托管bean获取selectonelistbox的值?

Bean来源:

    public void selectOneMenuListener(ValueChangeEvent event) {
    Object newValue = (String) event.getNewValue(); 
    selectedMenu = newValue.toString();
}

public String getSelectedMenu() {
    return selectedMenu;
}

public void setSelectedMenu(String selectedMenu) {
    this.selectedMenu = selectedMenu;
}

页:

    <h:panelGroup id="panel">
    <h:selectOneListbox id="katProduktu" valueChangeListener="#{produkt_KatBean.selectOneMenuListener}">
        <f:selectItems value="#{produkt_KatBean.produkt_KatAllList}"
            var="pk" itemLabel="#{pk.symbol}" itemValue="#{pk.id}"/>
        <f:ajax render="produktMenu" event="change" execute="@this" />
    </h:selectOneListbox>
</h:panelGroup>

<h:panelGroup layout="block" id="produktMenu">
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==1}">
        Menu 1
    </h:panelGroup>
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==2}">
        Menu 2
    </h:panelGroup>
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==3}">
        Menu 3
    </h:panelGroup>     
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==4}">
        Manu 4
    </h:panelGroup>
</h:panelGroup>

谢谢!

1 个答案:

答案 0 :(得分:0)

请勿使用valueChangeListener。这是工作的错误工具。只需将属性绑定到菜单组件的value属性,然后让rendered属性拦截。这样你根本不需要听众。您的rendered比较也不适用于字符串。您应该使用== '1'而不是== 1等等,但这很简单,只需将其设为Long(或Integer)。

<h:panelGroup id="panel">
    <h:selectOneListbox id="katProduktu" value="#{produkt_KatBean.selectedMenu}">
        <f:selectItems value="#{produkt_KatBean.produkt_KatAllList}"
            var="pk" itemLabel="#{pk.symbol}" itemValue="#{pk.id}"/>
        <f:ajax render="produktMenu" />
    </h:selectOneListbox>
</h:panelGroup>

<h:panelGroup layout="block" id="produktMenu">
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==1}">
        Menu 1
    </h:panelGroup>
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==2}">
        Menu 2
    </h:panelGroup>
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==3}">
        Menu 3
    </h:panelGroup>     
    <h:panelGroup rendered="#{produkt_KatBean.selectedMenu==4}">
        Manu 4
    </h:panelGroup>
</h:panelGroup>

只需

private Long selectedMenu; // Or Integer? Should be the same type as #{pk.id}.

// +getter +setter

(请注意,我省略了event的{​​{1}}和execute属性,因为它们已代表默认值