Facelets repeat Tag Index

58,744

Solution 1

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:

  • begin of type Integer
  • end of type Integer
  • index of type int
  • step of type Integer
  • even of type boolean
  • odd of type boolean
  • first of type boolean
  • last of type boolean

For more details, see:

https://docs.oracle.com/javaee/7/javaserver-faces-2-2/vdldocs-facelets/ui/repeat.html

Solution 2

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:

  1. Attributes for the UI:Repeat can be found here.
Share:
58,744
c12
Author by

c12

Updated on February 28, 2020

Comments

  • c12
    c12 about 4 years

    Does anyone know a way to get the index of the element in a ui:repeat facelets tag?

    <ui:repeat id="topTenGrd" var="dream" value="#{dreamModifyBean.topDreams}">
        <h:outputText class="dream-title uppercase" value="#{dream.number}. #{dream.title}" />
    </ui:repeat>