How to show null value in p:SelectOneMenu ONLY when said value is null in the backing bean?

浪尽此生 提交于 2019-12-12 02:09:30

问题


Here's my selectOneMenu:

<h:form>
    <p:selectOneMenu id="handlerSelect" value="#{caseController.case.handler}" 
        converter="omnifaces.SelectItemsIndexConverter" style="width:182px">
        <f:selectItems value="#{handlerController.findAllHandlers()}"
            var="handlerSelect" itemLabel="#{handlerSelect.name}"
            itemValue="#{handlerSelect}" />
        <p:ajax event="change"listener="#{caseController.changeHandler}" update="handlerSelect"/>
    </p:selectOneMenu>
</h:form>

The default value showing in this selectOneMenu as the user opens the view, is the name of the handler person of the case the user opens. Now, some cases have null as the value in the database. In those cases, the value is the name of the first handler person in the handler list. This is obviously wrong, since the value showing shouldn't be a handler person's name because the case in question doesn't have a handler person, but a null in the handler column of the case row.

Now, how can I show some custom text eg. "Choose handler" WHEN the handler property is null on the case object?


回答1:


To show a special "Choose handler" option, if your value is null, just add an additionally

<f:selectItem
  itemLabel="Choose handler"
  itemValue="#{null}"
/>

to your selectOneMenu.

To show this special option, only if your value is null, you can add an allmost identical second selectOneMenu but without the special option and give them both opposite render attributes. The resulting id problem can be solved by enclosing the selectOneMenus with e.g. <p:outputPanel /> having the original id attribute.

<h:form>
  <p:outputPanel id="handlerSelect">
    <p:selectOneMenu
      value="#{caseController.case.handler}" 
      converter="omnifaces.SelectItemsIndexConverter"
      style="width:182px"
      rendered="#{caseController.case.handler eq null}"
    >
      <f:selectItem
        itemLabel="Choose handler"
        itemValue="#{null}"
      />
      <f:selectItems 
        value="#{handlerController.findAllHandlers()}"
        var="handlerSelect"
        itemLabel="#{handlerSelect.name}"
        itemValue="#{handlerSelect}"
      />
      <p:ajax
        event="change"
        listener="#{caseController.changeHandler}"
        update="handlerSelect"
      />
    </p:selectOneMenu>
    <p:selectOneMenu
      value="#{caseController.case.handler}" 
      converter="omnifaces.SelectItemsIndexConverter"
      style="width:182px"
      rendered="#{caseController.case.handler ne null}"
    >
      <f:selectItems 
        value="#{handlerController.findAllHandlers()}"
        var="handlerSelect"
        itemLabel="#{handlerSelect.name}"
        itemValue="#{handlerSelect}"
      />
      <p:ajax
        event="change"
        listener="#{caseController.changeHandler}"
        update="handlerSelect"
      />
    </p:selectOneMenu>
  </p:outputPanel>
</h:form>


来源:https://stackoverflow.com/questions/38431712/how-to-show-null-value-in-pselectonemenu-only-when-said-value-is-null-in-the-ba

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