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

查看:130
本文介绍了根据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>

如果您想根据所选值执行一些业务逻辑,请使用<f:ajax listener>.

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天全站免登陆