hideNoSelectionOption in selectOneMenu is not working as expected

旧时模样 提交于 2019-12-19 11:54:23

问题


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"


来源:https://stackoverflow.com/questions/26258339/hidenoselectionoption-in-selectonemenu-is-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!