Facelets重复标签索引

41

有没有人知道如何在ui:repeat facelets标签中获取元素的索引?

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

99

指定"varStatus"属性的值:

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

您可以通过EL访问循环索引:

#{myVarStatus.index}

此外,以下属性可用于varStatus:

  • begin,类型为Integer
  • end,类型为Integer
  • index,类型为int
  • step,类型为Integer
  • even,类型为boolean
  • odd,类型为boolean
  • first,类型为boolean
  • last,类型为boolean

有关更多详细信息,请参见:

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


9
布莱恩的答案很好,但我认为它可以更详细地提供信息。
我们创建UI:Repeat。
<ui:repeat id="repeatOne" var="listofValues" varStatus="myVarStatus"> </ui:repeat>

使用UI Repeat,我们可以访问与列表“listofValues”相关联的变量的值。

使用varStatus,我们可以创建另一个变量来保存不同类型的信息。例如,在我们的列表中使用#{myVarStatus.index}来创建表格,我们可以使用此信息作为列表上的索引。

1.

2.

3.

当然,如果您将数组指定为从0开始,则列表也将如此,除非您为每个添加1。 #{myVarStatus.index + 1}

这些在需要使用嵌套的2个UI:Repeat的2D数组中也非常有用。

属性 ___Getter_________描述

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:Repeat的属性可以在这里找到。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接