hideNoSelectionOption in selectOneMenu is not work

2019-02-21 01:18发布

问题:

I have the following selectOneMenu and within of my component I want to have an item which shouldn't be shown, for e.g. in cases where the value from #{Mybean.value} match a value from #{Mybean.ListValues} I don't want to have an empty option in my combo box .

  <p:selectOneMenu value="#{Mybean.value}"  hideNoSelectionOption="true"     
   required="true" requiredMessage="Required data">

      <f:selectItem itemLabel="" itemValue="#{null}" noSelectionOption="true" />
      <f:selectItems value="#{Mybean.ListValues}" var="option"  itemLabel="#{option.optionName}"   
      itemValue="#{option.optionId}"/>
 </p:selectOneMenu>

I searched, but I didn't find anything useful, just one link in primefaces forum where describes exactly this problem.

My primefaces version is 3.5

回答1:

That attribute doesn't exist in the official api or in the doc. Where did you get it from?


What you're actually looking for is the itemDisabled attribute on the f:selectItems component. It's this attribute that disables a selectItem from being selected. Historically, primefaces has had problems with that attribute.

Ideally, you should have

   <p:selectOneMenu value="#{Mybean.value}" required="true" requiredMessage="Required data">
       <f:selectItem itemLabel="" itemValue="#{null}" noSelectionOption="true" />
       <f:selectItems itemDisabled="#{Mybean.value=='aValue'}" value="#{Mybean.ListValues}" var="option" itemLabel="#{option.optionName}"      itemValue="#{option.optionId}"/>
  </p:selectOneMenu>


回答2:

So, basically and based on @kolossus answer, we can in primefaces (when you are using <p:selectOneMenu...) case add the following empty item

<f:selectItem itemValue="#{null}" itemLabel="--select--"  itemDisabled="#{Mybean.value ne null}" />

We can in primefaces (when we have to use

Note: In such case we don't need the following two tags:

hideNoSelectionOption="true"

and

noSelectionOption="true"