Patan
Patan

Reputation: 17873

h:selectmanyListbox make "choose" option unselectable when at least one option is selected

Is it possible to have in <h:selectManyListbox> a default option like "--choose--" which can be selected when no option is selected. When the some value is chosen, then it must be unselectable.

<h:selectManyListbox value="#{bean.value}"
        class="form-control">
        <f:selectItems value="#{bean.dropdownValues}" var="value" itemLabel="#{value}" itemValue="#{value}"/>            
</h:selectManyListbox>

Upvotes: 1

Views: 1620

Answers (1)

BalusC
BalusC

Reputation: 1108632

Just add it as another <f:selectItem> and ask assistance of a bit of JavaScript to disable it when any value is selected during the change event.

<h:selectManyListbox ... onchange="options[0].disabled=!!value">
    <f:selectItem itemLabel="--choose--" itemValue="#{null}" />
    <f:selectItems ... />
</h:selectManyListbox>

The options[0] refers to the first option of the selection element. The !!value basically converts the selected item value to a boolean (which will be true when it's not empty/null), suitable for disabled attribute.

See also:

Upvotes: 1

Related Questions