Facelets重复标记索引

问题描述 投票:36回答:2

有没有人知道在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>
jsf jsf-2 facelets
2个回答
83
投票

为“varStatus”属性指定一个值:

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

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

#{myVarStatus.index}

此外,varStatus还可以使用以下属性:

  • 以Integer类型开头
  • 结束类型为整数
  • int类型的索引
  • 步骤类型为整数
  • 甚至是boolean类型
  • 奇数类型布尔值
  • 第一个类型为布尔值
  • boolean类型的最后一个

有关详细信息,请参阅:

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


6
投票

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的2D数组中也非常有用。

财产___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
© www.soinside.com 2019 - 2024. All rights reserved.