2011-04-06 14 views
30

¿Alguien sabe una manera de obtener el índice del elemento en una interfaz de usuario? ¿Repite la etiqueta facelets?Facelets repeat Tag Index

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

Respuesta

71

Especificar un valor para el atributo "varStatus":

<ui:repeat id="..." var="..." value="..." varStatus="myVarStatus"> 

A continuación, puede acceder al índice de bucle a través de EL:

#{myVarStatus.index} 

Además, las siguientes propiedades están disponibles para la varStatus:

  • begin of type Entero
  • final de tipo Integer
  • índice de tipo int
  • paso de tipo Integer
  • incluso de tipo booleano
  • impar de tipo booleano
  • primero de tipo booleano
  • último de tipo booleano

Para obtener más información, ver:

https://javaserverfaces.java.net/docs/2.2/vdldocs/facelets/ui/repeat.html

4

La respuesta de Brian es buena, pero creo que podría ser un poco más descriptiva para la información.

Creamos interfaz de usuario: Repetir

<ui:repeat id="repeatOne" var="listofValues" varStatus="myVarStatus"> </ui:repeat> 

Uso de la repetición de interfaz de usuario se puede acceder a los valores de la variable que asociado con la lista '' listofValues.

Usando varStatus podemos crear otra variable que contenga diferentes tipos de información. Por ejemplo, usando #{myVarStatus.index} en nuestra lista para crear una tabla, podemos usar esta información para nuestro índice en nuestra lista.

1.

2.

3.

Por supuesto, si se especifica la matriz de empezar en 0, entonces también lo hará su lista a menos que sumar 1 a cada uno. # {myVarStatus.index + 1}

Estos también son muy útiles en arreglos 2D que necesitan utilizar 2 UI:Repeat que están anidados.

Propiedad ___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 

Documentación adicional con enlaces:

  1. atributos de la interfaz de usuario: la repetición se puede encontrar here.