I was wondering what would be the best or easiest way to allow a user to select nothing in a selectOneMenu.
My example: I have a list of registered users and the administrator should be able to filter the list of displayed users by some criterias. These criterias, like the usertype (employee, customer, ...) can be chosen by selectOneMenus, like this:
<h:selectOneMenu value="#{myBean.selectedUsertype}" converter="#{usertypeConverter}">
<f:selectItems value={myBean.usertypes}" />
</h:selectOneMenu>
When the corresponding selectOneMenu is being backed by a list of POJOs using a converter, how can I add an item to the list indicating that the user didn't choose any specific item? Currently I have a dummy usertype object displaying the label "---", but this is causing several problems in other areas of my application and I don't think that this is the best solution.
We can in primefaces (when we have to use
<p:selectOneMenu
... from some reason like using<p:ajax
..) add the following empty item:Note: In such case we don't need the following two tags:
and
Just explicitly set the select item value to
null
.No, an empty string like
itemValue=""
is not sufficient. It really has to benull
. Otherwise you run into trouble as described in this Q&A: Using a "Please select" f:selectItem with null/empty value inside a p:selectOneMenu.If the item happen to be
required="true"
and you're using JSF 2.x, then you could addnoSelectionOption="true"
to the select item. If you also sethideNoSelectionOption="true"
on the selection component, then it will hide the empty option in the list once the enduser selects a different item, hereby making it impossible to re-select the empty option.Add a single selectItem with null value;