根据 selectOneMenu 值有条件地渲染组件 [英] Conditionally render a component based on selectOneMenu value

查看:32
本文介绍了根据 selectOneMenu 值有条件地渲染组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法根据用户从 selectOneMenu 组件中选择的当前值来呈现组件?我的 selectOneMenu 组件填充了一个包含两个值的枚举,吸烟者和非吸烟者.如果用户选择了吸烟者,我想在其下方呈现一个复选框,让用户检查他们每天吸烟 10、20、30+ 等的数量.如果他们选择了非吸烟者,我也希望相反的工作,即复选框不会呈现/消失.

Is there a way to render a component based on the current value the user has selected from a selectOneMenu component? My selectOneMenu component is populated with an enum consisting of two values, smoker and non-smoker. If the user has smoker selected I want to render a checkbox below it which lets the user check how many they smoke a day 10, 20, 30+, etc. I also want the opposite to work if they user has selected non-smoker i.e. the check boxes don't render/disappear.

推荐答案

只需在目标组件的 rendered 属性中检查下拉菜单的值,并通过 < 更新它们的公共父级.f:ajax>.这是一个启动示例:

Just check the dropdown menu's value in the rendered attribute of the target components and update their common parent by a <f:ajax>. Here's a kickoff example:

<h:selectOneMenu value="#{bean.item}">
    <f:selectItem itemValue="one" />
    <f:selectItem itemValue="two" />
    <f:selectItem itemValue="three" />
    <f:ajax render="results" />
</h:selectOneMenu>

<h:panelGroup id="results">
    <h:panelGroup rendered="#{bean.item eq 'one'}">
        You have selected "one".
    </h:panelGroup>
    <h:panelGroup rendered="#{bean.item eq 'two'}">
        You have selected "two".
    </h:panelGroup>
    <h:panelGroup rendered="#{bean.item eq 'three'}">
        You have selected "three".
    </h:panelGroup>
</h:panelGroup>

如果您想根据选定的值执行一些业务逻辑,请使用 .

If you'd like to perform some business logic based on the selected value, use <f:ajax listener>.

<f:ajax listener="#{bean.changeItem}" render="results" />

public void changeItem() {
    someResult = someService.getByItem(item);
}

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆