问题
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