When creating a new item the user does not want to see the null no selection option in the Select-One Menu, but directly an item from the available items. So either to get rid of the null no option or display directly the second element in the dropdown. I tried the attribute hideNoSelection, but it does nothing.
How can I achieve this?
<h:selectOneMenu id="ugroup" value="#{usersController.selected.ugroup}" title="#{bundle.CreateUsersTitle_ugroup}" required="true" requiredMessage="#{bundle.CreateUsersRequiredMessage_ugroup}">
</h:selectOneMenu>
—————-EDIT (after @Balus Answer) ———————
Here is the Utility method that grabs JPA Lists to be displayed as a <h:selectOneMenu /> :
public class JsfUtil {
public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
int size = selectOne ? entities.size() + 1 : entities.size();
SelectItem[] items = new SelectItem[size];
int i = 0;
if (selectOne) { // part to be removed
items[0] = new SelectItem("", "---");
i++;
} // end of part to be removed
for (Object x : entities) {
items[i++] = new SelectItem(x, x.toString());
}
return items;
}
So appearently, all I have to do is remove this part? :
if (selectOne) {
items[0] = new SelectItem("", "---");
i++;
}
But as this static method is used everywhere in my code, would it have any unwanted consequences?
-
Like, when the user wants explicitly a null value from the
<h:selectOneMenu />, if he decides to choose no option. My guess was to use<f:selectItem:
Just ensure that
#{ugroupController.itemsAvailableSelectOne}does not contain an item with valuenullin the array or list. The first item will be the default selected option.Alternatively, preset the property behind
#{usersController.selected.ugroup}with the value of the desired item during bean’s (post)construction.