Marko Ćilimković
Marko Ćilimković

Reputation: 753

How to change OneMenu element to outputText in JSF

I would like my page to be user friendly as much as possible and I have an idea but its a little bit harder in the way I want to solve it.

I'm using primefaces and I would like to have a selectOneMenu element which changes to just an outputText with the value of the selected variable in the selectOneMenu. Anyone have some nifty ideas?

Upvotes: 1

Views: 522

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

This can be done with ajax and partial rendering. Here is a sketch of my idea (untested and in plain JSF):

<h:panelGroup id="wrapper">
  <h:selectOneMenu value="#{myBean.myValue}" 
                   rendered="#{myBean.myValue == someInitialValue}" ...>
     <f:ajax render="wrapper"/>
       ... (your select items here)
  </h:selectOneMenu>
  <h:outputText value="#{myBean.myValue}" 
                rendered="#{myBean.myValue != someInitialValue}" .../>
</h:panelGroup>

And that's what it does:

The value of h:selectOneMenu will be initialized and the menu will be rendered only if it is the initial value.

On change of the value, the surrounding panelgroup will be re-rendered, hides the menu and lets the h:outputText appear.

You need a wrapping panelGroup for this because the outputText is not there at page load. If your form is small you could also render=@form or any other surrounding container instead. Then you wouldn't need the wrapper.

Upvotes: 1

Related Questions