1

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Patan
  • 17,073
  • 36
  • 124
  • 198

1 Answers1

1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the answer. I tried the above solution. But I cannot select the --choose-- option once I select the valid option. Am I missing any thing. – Patan Jun 18 '15 at 08:53
  • But you explicitly mentioned *"then it shall not be able to chose default dummy value."* Did I misunderstand it? – BalusC Jun 18 '15 at 12:28
  • Sorry for the confusion. I actually mean, when the user selects any valid option then default option shall be visible. user shall not be able to select dummy option until he deselect any other valid option. Consider that listbox is not mandatory. I would want leave the drop down without selecting any option. – Patan Jun 18 '15 at 13:13
  • So, making it always unselectable (simply by using `itemDisabled="true"`) is thus also not an option? – BalusC Jun 18 '15 at 13:16
  • Yeah correct. Field is non mandatory user can select valid option or leave with dummy any time – Patan Jun 18 '15 at 13:17