Conditionally render a component based on selectOneMenu value

后端 未结 1 1669
野的像风
野的像风 2020-12-06 23:27

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 c

相关标签:
1条回答
  • 2020-12-06 23:41

    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:

    • Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
    • How to load and display dependent h:selectOneMenu on change of a h:selectOneMenu
    • Conditionally displaying JSF components
    0 讨论(0)
提交回复
热议问题