Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jsf - Conditionally render a component based on selectOneMenu value

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...