jsfjsf-2selectoneradio

How to get index of selected item of jsf f:selectItems?


I have seleconeradio, for example:

<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
    <f:selectItems value="#{myBean.myList}" var="a" itemValue="#{a}" itemLabel="#{a}"/>
</h:selectOneRadio>

where myList is list of integers, e.g. 1,3,2,4. If user selects second element (i.e. 3) I want in myBean selectedValue to be 2, so I want to get index of selectItems item.

What should I write in f:selectItems itemValue tag? Or it is impossible?

P.S. I can do it by creating a new class in which I have the index property and create a new list of that class, giving the right index. But it is very bad solution.


Solution

  • you should use indexOf(Object o) .. it returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element... your code should probably look like this..

    int index  = myList.indexof(selectedValue);