添加“未选择内容"的最佳方法JSF中selectOneMenu的选项 [英] Best way to add a "nothing selected" option to a selectOneMenu in JSF

查看:142
本文介绍了添加“未选择内容"的最佳方法JSF中selectOneMenu的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道让用户在selectOneMenu中什么都不选择的最佳或最简单方法是什么.

I was wondering what would be the best or easiest way to allow a user to select nothing in a selectOneMenu.

我的示例:我有一个注册用户列表,管理员应该能够按照某些条件过滤显示的用户列表.这些条件,例如用户类型(员工,客户等)可以通过selectOneMenus进行选择,如下所示:

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>

当使用转换器通过POJO列表支持相应的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.

推荐答案

只需将选择项的值显式设置为null.

Just explicitly set the select item value to null.

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItem itemValue="#{null}" itemLabel="--select--" />
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

否,像itemValue=""这样的空字符串是不够的.确实必须为null.否则,您会遇到此问答中所述的麻烦:中具有空值/空值.

No, an empty string like itemValue="" is not sufficient. It really has to be null. 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.

如果该项目恰好是required="true",并且您正在使用JSF 2.x,则可以将noSelectionOption="true"添加到选择的项目中.仅当您在选择组件上设置了hideNoSelectionOption="true"时,此功能才有用.一旦最终用户选择了其他选项,它将在列表中隐藏该空白选项,从而使得无法重新选择该空白选项.

If the item happen to be required="true" and you're using JSF 2.x, then you could add noSelectionOption="true" to the select item. This is only useful if you also set hideNoSelectionOption="true" on the selection component. It will then hide the empty option in the list once the enduser selects a different item, hereby making it impossible to re-select the empty option.

<h:selectOneMenu value="#{bean.selectedItem}" hideNoSelectionOption="true">
    <f:selectItem itemValue="#{null}" itemLabel="--select--" noSelectionOption="true" />
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

这篇关于添加“未选择内容"的最佳方法JSF中selectOneMenu的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆