When I use an empty SelectItem in a SelectOneMenu to provide my users with an empty choice in the SelectOneMenu it works fine. As was suggested in this thread: Best way to add a "nothing selected" option to a selectOneMenu in JSF
But when I use an empty SelectItem in a SelectOneMenu which resides in a datable it does not persist the correct SelectItem when I use CellEditing in the data table.
Here is my code:
<p:dataTable id="fooTable"
value="#{fooSessionBean.foos}"
var="foo"
rows="15"
style="width: 100%"
paginator="true"
paginatorPosition="bottom"
paginatorAlwaysVisible="false"
editable="true" >
...
<p:column sortBy="#{empty foo.bar ? '' : foo.bar.name}">
<p:cellEditor>
<f:facet name="output"> <h:outputText value="#{foo.bar.name}" title="#{foo.bar.name}" rendered="#{!empty foo.bar}" /></f:facet>
<f:facet name="input">
<p:selectOneMenu id="bars" value="#{foo.bar}" converter="foobarConverter" style="width:100%">
<f:selectItem itemLabel="No bar" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{fooSessionBean.foos}" var="bars" itemLabel="#{bars.name}" itemValue="#{bars}" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
...
<p:column style="width:10%">
<p:rowEditor />
<p:commandButton action="#{fooSessionBean.delete(foo.id)}" icon="ui-icon-trash" title="remove" ajax="false"/>
</p:column>
A Foo object has a field Bar. The code above generates a list of Bar items when you try to edit the field using RowEditing. It is also possible for the Foo object to have no Bar field. Hence the empty SelectItem.
This all works except that the wrong Bar item is persisted in Foo object. The Bar object before the one I have selected is persisted. eg:
The SelectOneMenu contains:
- emptybar
- bar1
- bar2
- bar3
When you select bar2, bar1 will be persisted in te foo object.
Is it possible to use the different components in such a away or am I overlooking something else? Because I have found little references of people attempting this and having the same problem I am having.