开发者

<h:selectOneMenu> with temporary disabled SelectItems

开发者 https://www.devze.com 2023-02-10 11:00 出处:网络
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

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.

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?

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.

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 no开发者_Python百科t submitted so I get a null value in my model which is also wrong.

I am stuck. Any advice is kindly appreciated.

Best regards, Dan.


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>

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.


"that means that the value is still valid but should not be used by the user"

Help me understand this piece. You want to display these choices in an h:selectOneMenu but not allow the user to select them? I guess I don't understand why you would present them to the user if they are invalid options for them?

You could always create a validator that validates against "temporaryDisabled" if that's what you're trying to accomplish ... let me know more about what you want the end-user to see and I can probably help.


<h:selectOneMenu 
    id="cardStatus" 
    value="#{someBean.cardStatus}"
    converter="selectItemConverter">
    <f:selectItem itemValue="#{null}" itemLabel="" />
    <f:selectItems  
        value="#{cardStatusBean.cardStatuses}"
        var="cardStatus"
        itemValue="#{cardStatus}"
        itemLabel="#{cardStatus.name}"
        itemDisabled="#{cardStatus.temporaryDisabled}"/>    
</h:selectOneMenu>

This would give you a default choice with a value of null and an empty label. Would that accomplish what you are looking for?


For the record, if using JSF 2.0, f:selectItem and f:selectItems have an "itemDisabled" attribute that produces the desired behavior.

0

精彩评论

暂无评论...
验证码 换一张
取 消