仅当备用Bean中的空值为null时,才如何在p:SelectOneMenu中显示空值? [英] How to show null value in p:SelectOneMenu ONLY when said value is null in the backing bean?

查看:63
本文介绍了仅当备用Bean中的空值为null时,才如何在p:SelectOneMenu中显示空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的selectOneMenu:

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>

用户打开视图时此selectOneMenu中显示的默认值是用户打开案例的处理人员的名称.现在,某些情况下,null作为数据库中的值.在这些情况下,该值是处理程序列表中第一个处理程序人员的名称.这显然是错误的,因为显示的值不应该是处理人员的名字,因为所讨论的案例没有处理人员,而案例行的处理程序列中为null.

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.

现在,如何显示一些自定义文本,例如.当案例对象上的处理程序属性为null时,选择处理程序"?

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

推荐答案

要显示特殊的选择处理程序"选项,如果您的值为 null ,则只需添加一个额外的

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

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

到您的selectOneMenu.

to your selectOneMenu.

要显示此特殊选项,仅当您的值为 null 时,才可以添加一个完全相同的第二个selectOneMenu,但不包含特殊选项,并为其赋予两个相反的渲染属性.产生的id问题可以通过将selectOneMenus括在例如<p:outputPanel />具有原始id属性.

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>

这篇关于仅当备用Bean中的空值为null时,才如何在p:SelectOneMenu中显示空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆