Does anyone know a way to get the index of the element in a ui:repeat facelets tag?
The answer by Brian is good but I think it could be a bit more descriptive for information.
We create UI:Repeat
<ui:repeat id="repeatOne" var="listofValues" varStatus="myVarStatus"> </ui:repeat>
Using UI Repeat we can access the values from the variable we associated with the list 'listofValues'.
Using varStatus we can create another variable that holds different type of information. For example using #{myVarStatus.index}
in our list to create a table we can use this information for our index on our list.
1.
2.
3.
Of course if you specify your array to start at 0 then so will your list unless you add 1 to each. #{myVarStatus.index + 1}
These are also very useful in 2D arrays that need to use 2 UI:Repeat
that are nested.
Property ___Getter_________Description
current getCurrent() The item (from the collection) for the current round of iteration
index getIndex() The zero-based index for the current round of iteration
count getCount() The one-based count for the current round of iteration
first isFirst() Flag indicating whether the current round is the first pass through the iteration
last isLast() Flag indicating whether the current round is the last pass through the iteration
begin getBegin() The value of the begin attribute
end getEnd() The value of the end attribute
step getStep() The value of the step attribute
Additional Documentation with links:
Specify a value for the "varStatus" attribute:
<ui:repeat id="..." var="..." value="..." varStatus="myVarStatus">
You can then access the loop index via EL:
#{myVarStatus.index}
Additionally, the following properties are available to the varStatus:
For more details, see:
https://docs.oracle.com/javaee/7/javaserver-faces-2-2/vdldocs-facelets/ui/repeat.html