How can I specify a conditional rendering for an <f:selectItem> tag.
I need to display <f:selectItem> options according to a specific user’s status.
For example, I wanted something like:
<f:selectItem itemLabel="Yes! I need a girlfriend!"
rendered="false(or some boolean condition)"
itemValue="o1"/>
The
<f:selectItem>does not support therenderedattribute. Your closest bet is theitemDisabledattribute which still displays the item, but makes it unselectable. This is also supported in<f:selectItems>.In case of
<p:selectOneMenu>you can then just add some CSS to hide disabled items.In case of
<h:selectOneMenu>you’re more dependent on whether the webbrowser supports hiding the disabled options via CSS:The server side alternative is to bring in a JSTL
<c:if>around the individual<f:selectItem>to contitionally add it to the view like this (make sure you’re aware of how JSTL works in JSF: JSTL in JSF2 Facelets… makes sense?):Or, you could simply dynamically populate a
List<SelectItem>in the backing bean based on the calculated conditions and bind it with<f:selectItems>.