6

I have a page with a <h:selectOneMenu> and I want to show some fields or others depending on the chosen value of the selectOneMenu. Is this possible and if so, how?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
BRabbit27
  • 6,333
  • 17
  • 90
  • 161

1 Answers1

24

Yes, this is surely possible. Just bind the dropdown's value to the rendered attribute of the components to be shown/hidden. Here's a kickoff example.

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

    <h:panelGroup rendered="#{bean.item == 'one'}">
        <p>This will be shown if the selected item equals to "one".</p>
    </h:panelGroup>

    <h:panelGroup rendered="#{bean.item == 'two'}">
        <p>This will be shown if the selected item equals to "two".</p>
    </h:panelGroup>

    <h:panelGroup rendered="#{bean.item == 'three'}">
        <p>This will be shown if the selected item equals to "three".</p>
    </h:panelGroup>
</h:form>

The <h:panelGroup> is just examplary. It can be every JSF HTML component, such as <h:inputText> or even another <h:selectOneMenu>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I've been working on this, there's an option in my **selectOneMenu** where I want to display a **** to list files on a dir I've already the code for the tree, and test it isolated, but when I put that same code in one of the options it does not get rendered. – BRabbit27 Nov 14 '11 at 15:07
  • Can I nest a form within a form?, I mean, replace the `` for ``. – BRabbit27 Nov 14 '11 at 17:39
  • No, that's invalid in HTML, so you should also not do it in JSF. – BalusC Nov 14 '11 at 17:42
  • Maybe thats why it isnt rendering. So I just add the **tree2** code without a the form tags? – BRabbit27 Nov 14 '11 at 19:34
  • As long as you don't nest forms. – BalusC Nov 14 '11 at 19:36
  • Hi. If I get rid of the **** tags, and I put outside **** three forms that will correspond to the options in the **selectOneMenu**, can I render the new forms using ajax like `` or ` – BRabbit27 Nov 15 '11 at 00:19
  • My answer was just a kickoff example to give you new insights. You can do with it whatever you want. You can replace `` by any JSF HTML component which supports the `rendered` attribute. You can position them everywhere you want. As long as it suits your functional requirements. If you worry about performance, run a profiler. If you have a new question, ask a new and concrete question by `Ask Question` button :) – BalusC Nov 15 '11 at 00:23
  • @BalusC do we need ``? what its doing? – Darshana Feb 26 '14 at 06:28