【问题标题】:Facelets repeat Tag IndexFacelets 重复标签索引
【发布时间】:2011-04-06 06:21:07
【问题描述】:

有谁知道在 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


    【解决方案1】:

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

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

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

    #{myVarStatus.index}
    

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

    • 整数类型开始
    • 整数类型结束
    • int 类型的索引
    • 整数类型的步骤
    • 甚至是布尔类型
    • 布尔类型的奇数
    • 布尔类型的第一个
    • 最后一个布尔类型

    更多详情见:

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

    【讨论】:

      【解决方案2】:

      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_________描述

      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. 可以在here 找到 UI:Repeat 的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-25
        • 2015-04-24
        • 2018-06-22
        • 1970-01-01
        相关资源
        最近更新 更多