Facelets repeat Tag Index
Asked Answered
C

2

42

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>
Charleen answered 6/4, 2011 at 6:21 Comment(0)
C
99

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

Charlsiecharlton answered 6/4, 2011 at 6:50 Comment(0)
R
9

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.
Renewal answered 14/11, 2016 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.