CustomContent in Primefaces selectOneMenu without persisting object

你说的曾经没有我的故事 提交于 2019-12-24 09:58:59

问题


I am using the Primefaces SelectOneMenu (http://www.primefaces.org/showcase-labs/ui/selectOneMenu.jsf) with the "Custom content" version as described in the demo.

The problem is that the columns only work if the actual value of the field, which is bound to the dropdown field, is of the type of the Object used as the value.

The following works (if Bean.field is of type SelectObject):

<p:selectOneMenu value="#{bean.field}"
    var="x">
    <f:selectItems
        value="#{valuesBean.getSelectItems()}" var="bt"
        itemLabel="#{bt.label}" itemValue="#{bt}" />
    <p:column>
        <h:outputText
            value="#{x.value}" />
    </p:column>
    <p:column>
        <h:outputText value="xyz" />
    </p:column>
</p:selectOneMenu>

The following does NOT work (Bean.field of type String):

<p:selectOneMenu value="#{bean.field}"
    var="x">
    <f:selectItems
        value="#{valuesBean.getSelectItems()}" var="bt"
        itemLabel="#{bt.label}" itemValue="#{bt.value}" />
    <p:column>
        <h:outputText
            value="#{x.value}" />
    </p:column>
    <p:column>
        <h:outputText value="xyz" />
    </p:column>
</p:selectOneMenu>

In the first version, the xyz is displayed as second column, in the second version it's not. The problem I have is that now I need to save an object of type SelectObject in the database although I could as well just save the String of the value field there.

Is there a nice way to handle this?


回答1:


Use a Converter to convert the value of the selected menu item (bt.value) to that expected by the backing bean method tied to the p:selectOneMenu (bean.field).

<p:selectOneMenu value="#{bean.field}" var="x" converter="yourconverterid">


来源:https://stackoverflow.com/questions/11396172/customcontent-in-primefaces-selectonemenu-without-persisting-object

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