< h:selectOneMenu>临时禁用的SelectItems [英] <h:selectOneMenu> with temporary disabled SelectItems

查看:153
本文介绍了< h:selectOneMenu>临时禁用的SelectItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<h:selectOneMenu>,它从我的对象模型中渲染cardStatus的值. CardStatus具有布尔属性'temporaryDisabled',这意味着该值仍然有效,但不应由用户使用.

I have a <h:selectOneMenu> that renders the value of the cardStatus from my object model. A CardStatus has an boolean attribute 'temporaryDisabled' that means that the value is still valid but should not be used by the user.

现在,如果我的模型将cardStatus设置为临时禁用值,那么如何在下拉组合框中显示该值,并且仍然阻止用户将其更改为另一个临时禁用状态?

Now, if my model has cardStatus set to a temporary disabled value, how can I show this value in the dropdown combobox and still prevent the user from changing the value to another temporary disabled status?

如果我只是从我提供给<h:selectOneMenu>的SelectItems列表中删除了禁用的卡状态,那么当呈现选择时,它将自动选择列表中的第一项并在下次提交,因此错误地更改了我的值模型.

If I just delete the disabled card statuses from the list of SelectItems that I feed to <h:selectOneMenu> then when the select gets rendered it will automatically select the first item in the list an submit it next time consequently wrongly changing my value in the model.

如果我在SelectItems列表中包括了禁用的卡状态,但是将其对应项的disabled属性的值设置为true,则它们将以HTML禁用的形式呈现,并且不会提交,因此我在模型中得到的值为空也错了.

If I include the disabled card statuses in the list of SelectItems but set the value of the disabled attribute to true for their corresponding items, they are rendered in HTML disabled and not submitted so I get a null value in my model which is also wrong.

我被困住了.如有任何建议,我们将不胜感激.

I am stuck. Any advice is kindly appreciated.

最诚挚的问候, 丹.

推荐答案

最后,我要做的是使用一段jQuery代码,该代码在页面加载后执行.

Finally what I did was to use a piece of jQuery code that gets executed after the page is loaded.

<h:selectOneMenu 
    id="cardStatus" 
    value="#{someBean.cardStatus}"
    converter="selectItemConverter">
    <f:selectItem itemValue="E|A" itemLabel="Active" />
    <f:selectItem itemValue="E|S" itemLabel="Stolen" />
    <f:selectItem itemValue="D|B" itemLabel="Blocked" />
    <f:selectItem itemValue="E|L" itemLabel="Lost" />
    <f:selectItem itemValue="D|C" itemLabel="Counterfeit" />
</h:selectOneMenu>

javascript代码的作用是扫描所有项目,并为每个项目添加以前缀 D | 开头的值,并使用jQuery的hide()函数隐藏该项目.这样,组合框就可以充当所有值,因为它们都是有效/启用的,但是用户将无法选择不活动的值,因为它们是不可见的.此外,如果默认选择的值是以 D | 开头的值之一,则该值仍将显示为默认值,但用户无法在他/她的选项列表中看到它可以选择.

What the javascript code does is to scan all the items and for each item with a value starting with the prefix D| hide the item using jQuery's hide() function. This way the combobox is acting as all values would be valid/enabled, but the user will not be able to select inactive values because they are not visible. Furthermore, if the default selected value is one of the values starting with D|, the value will still be shown as default value but the user is not able to see it in the list of options he/she can choose from.

这篇关于&lt; h:selectOneMenu&gt;临时禁用的SelectItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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