Steve Waters
Steve Waters

Reputation: 3548

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

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?

Upvotes: 1

Views: 1608

Answers (1)

irieill
irieill

Reputation: 1252

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>

Upvotes: 2

Related Questions