2

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 not 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.

Dan Corneanu
  • 336
  • 2
  • 11

4 Answers4

1

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.

Dan Corneanu
  • 336
  • 2
  • 11
0

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

Tobias
  • 43
  • 4
0

"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.

Dave Maple
  • 8,102
  • 4
  • 45
  • 64
  • 1
    He want to make certain items unselectable. – BalusC Feb 17 '11 at 15:07
  • The idea is that I want a combobox with a slightly modified behavior. The default behavior for a combobox is to let the user choose from a list of items and if no ` – Dan Corneanu Feb 18 '11 at 07:54
0
<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?

Dave Maple
  • 8,102
  • 4
  • 45
  • 64
  • Unfortunately this is not what I am looking for. Actually I want the possibility of having as default value one of the disabled items and on submit have this value sent to the server and filled into my model (just in case the user has not touched the comobox). – Dan Corneanu Feb 21 '11 at 10:52
  • Can you just add the value you want to submit as the f:selectItem. ? That way if the selectOneMenu is not touched this value will be the one submitted. – Dave Maple Feb 21 '11 at 14:35