I have bilateral One to Many Relationship in a JSF + JPA application. I want to list out only filtered items from the list in the view. Doing it in the controller is difficult a
The PrimeFaces <p:selectOneListbox>
actually hides the <select><option>
and generates a <div><ul><li>
which allows you much more CSS styling freedom.
You can just make use of itemDisabled
attribute and then use CSS to set disabled items to display: none
.
<p:selectOneListbox ...>
<f:selectItems value="#{investigationItemController.currentInvestigation.reportItems}"
var="ri" itemLabel="#{ri.name}" itemValue="#{ri}"
itemDisabled="#{not ri.retired and ri.ixItemType eq 'Value'}" />
</p:selectOneListbox>
with this CSS
.ui-selectlistbox-item.ui-state-disabled {
display: none;
}
As to your failed attempt with <ui:repeat>
; it failed because the <f:selectItem>
is evaluated during view build time, not during view render time. However, the <ui:repeat>
runs during view render time. During that moment the <f:selectItem>
is nowhere available in the component tree. It's supposed to be attached to an UISelectOne
/UISelectMany
parent during view build time.
JSTL runs during view build time, like <f:selectItem>
, so using JSTL <c:forEach>
for the loop and <c:if>
for the conditional building (not rendering!) should do it as well:
<p:selectOneListbox ...>
<c:forEach items="#{investigationItemController.currentInvestigation.reportItems}" var="ri">
<c:if test="#{not ri.retired and ri.ixItemType eq 'Value'}">
<f:selectItem itemLabel="#{ri.name}" itemValue="#{ri}" />
</c:if>
</c:forEach>
</p:selectOneListbox>
You only need to take into account that this breaks view scoped beans when using a Mojarra version older than 2.1.18.