Reputation: 2205
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.
Upvotes: 0
Views: 6817
Reputation: 422
You can actually use c:forEach
for this case. This is especially usefull when you have to deal with a collection containing duplicates and therefore can't use indexOf()
for example.
<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
<c:forEach items="#{myBean.myList}" var="a" varStatus="idx">
<f:selectItem itemValue="#{idx.index}" itemLabel="#{a}"/>
</c:forEach>
</h:selectOneRadio>
Just be sure to include the JSP JSTL Core namespace if you haven't done yet.
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core
Upvotes: 6
Reputation: 698
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);
Upvotes: 0