jsf - <h:selectOneMenu with conditional <f:selectItems shows options twice -


i want use selectonemenu have user choose value. in cases want disable 1 of values shown in menu. tried using render on both selectitems selectonemenu added ui:fragment around menu values both lists shown. ideas how prevent that? here current last try again resulted in twice list , item in question once enabled , once disabled in it:

<ui:fragment rendered="#{cc.attrs.showp==true}">     <h:selectonemenu id="type" binding="#{cc.type}">         <f:selectitems value="#{typedao.findall()}"/>     </h:selectonemenu> </ui:fragment> <ui:fragment rendered="#{cc.attrs.showp==false}">     <h:selectonemenu id="type" binding="#{cc.type}">         <f:selectitems value="#{typedao.findall()}" var="item" itemdisabled="#{item=='p'}"/>     </h:selectonemenu> </ui:fragment> 

your concrete problem caused because you're binding physically multiple components same variable.

<h:selectonemenu ... binding="#{cc.type}" /> <h:selectonemenu ... binding="#{cc.type}" /> 

if getter behind binding returns non-null, jsf use instead of creating new one. basically, second tag reuse component created in first tag , set/add attributes/items it.

your particular case can solved in @ least 2 ways:

  1. use jstl build jsf component tree conditionally instead of using jsf render html output conditionally. shouldn't have physically multiple components in jsf component tree sharing same binding let alone same id.

    <c:if test="#{cc.attrs.showp}">     <h:selectonemenu id="type" binding="#{cc.type}">         ...     </h:selectonemenu> </c:if> <c:if test="#{not cc.attrs.showp}">     <h:selectonemenu id="type" binding="#{cc.type}">         ...     </h:selectonemenu> </c:if> 
  2. make code dry. i.e. rid of code duplication.

    <h:selectonemenu id="type" binding="#{cc.type}">     <f:selectitems value="#{typedao.findall()}" var="item" itemdisabled="#{not cc.attrs.showp , item eq 'p'}" /> </h:selectonemenu> 

see also:


Comments