2011-04-06 58 views
30

有沒有人知道一種方法來獲取ui中元素的索引:repeat facelets tag?Facelets重複標籤索引

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

回答

71

對於 「varStatus」 屬性指定一個值:

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

然後,可以通過EL訪問循環索引:

#{myVarStatus.index} 

此外,以下的屬性可用於將varStatus:

  • 開始的類型Integer類型整數的
  • int類型的索引
  • Integer類型的步驟
  • 甚至類型布爾
  • 奇數布爾類型
  • 第一類型布爾
  • 最後布爾類型

欲瞭解更多詳情,請參閱:

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

4

Brian的回答很好,但我認爲它對信息可能更具描述性。

我們創建UI:重複

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

使用UI重複,我們可以從我們跟名單「listofValues」關聯的變量訪問值。

使用varStatus我們可以創建另一個變量來保存不同類型的信息。例如,在我們的列表中使用#{myVarStatus.index}來創建表格,我們可以將這些信息用於我們列表中的索引。

1.

2.

3.

如果您指定數組0開始當然再等會你的列表,除非你加1,每個。 #{myVarStatus.index + 1}

這些對於需要使用嵌套的2 UI:Repeat的二維數組也非常有用。

物業___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 

的鏈接其他文檔:

  1. 屬性的UI:重複可以發現here