• cdi
  • components
  • contexts
  • converters
  • el
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • push
  • resourcehandlers
  • scripts
  • servlets
  • taghandlers
  • utils
  • validators
  • viewhandlers
 - 
  • GenericEnumConverter
  • ListConverter
  • ListIndexConverter
  • SelectItemsConverter
  • SelectItemsIndexConverter
  • ToCollectionConverter
  • ToLowerCaseConverter
  • ToUpperCaseConverter
  • TrimConverter
  • ValueChangeConverter

Available since OmniFaces 1.3

The omnifaces.SelectItemsIndexConverter is a variant of the SelectItemsConverter which automatically converts based on the position (index) of the selected item in the list instead of the Object.toString() of the selected item.

Usage

This converter is available by converter ID omnifaces.SelectItemsIndexConverter. Just specify it in the converter attribute of the selection component holding <f:selectItems>.

<h:selectOneMenu value="#{bean.selectedItem}" converter="omnifaces.SelectItemsIndexConverter">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

Pros and cons as compared to SelectItemsConverter

This converter has the following advantages over SelectItemsConverter:

This converter has the following disadvantage over SelectItemsConverter:

  • The "Validation Error: value is not valid" will never occur anymore for the case that the available select items has incompatibly changed during the postback due to a developer's mistake. The developer should make absolutely sure that exactly the same list is preserved on postback (e.g. by making it a property of a view scoped or broader scoped bean).
Demo

Last selected item:

Demo source code
<h:form>
    <p>
        <b>Last selected item:</b> <h:outputText id="selected_item" value="#{selectItemsBean.selectedEntity.value}" />
    </p>
    
    <h:panelGrid columns="2">
        <h:outputLabel for="iterator" value="Items with iterator: " />
        <h:selectOneMenu id="iterator" value="#{selectItemsBean.selectedEntity}" converter="omnifaces.SelectItemsIndexConverter">
            <f:selectItem itemValue="#{null}" itemLabel="Choose item" noSelectionOption="true" />
            <f:selectItems value="#{selectItemsBean.exampleEntities}" var="entity" itemLabel="#{entity.value}" itemValue="#{entity}" />
            <f:ajax render="selected_item" />
        </h:selectOneMenu>

        <h:outputLabel for="iterator_def_value" value="Items with iterator (default value): " />
        <h:selectOneMenu id="iterator_def_value" value="#{selectItemsBean.selectedEntity}" converter="omnifaces.SelectItemsIndexConverter">
            <f:selectItem itemLabel="Choose item" noSelectionOption="true" />
            <f:selectItems value="#{selectItemsBean.exampleEntities}" var="entity" itemLabel="#{entity.value}" />
            <f:ajax render="selected_item" />
        </h:selectOneMenu>

        <h:outputLabel for="selectitems" value="Items with SelectItem list: " />
        <h:selectOneMenu id="selectitems" value="#{selectItemsBean.selectedEntity}" converter="omnifaces.SelectItemsIndexConverter">
            <f:selectItem itemLabel="Choose item" noSelectionOption="true" />
            <f:selectItems value="#{selectItemsBean.selectItems}" />
            <f:ajax render="selected_item" />
        </h:selectOneMenu>

        <h:outputLabel for="selectitems_array" value="Items with SelectItem array: " />
        <h:selectOneMenu id="selectitems_array" value="#{selectItemsBean.selectedEntity}" converter="omnifaces.SelectItemsIndexConverter">
            <f:selectItem itemLabel="Choose item" noSelectionOption="true" />
            <f:selectItems value="#{selectItemsBean.selectItemArray}" />
            <f:ajax render="selected_item" />
        </h:selectOneMenu>
    </h:panelGrid>
</h:form>