【问题标题】:How do i set end value and begin value dynamically in c:forEach loop?如何在 c:forEach 循环中动态设置结束值和开始值?
【发布时间】:2012-10-10 15:35:12
【问题描述】:

下午好。

我有以下问题,属性值结束

<c: foreach should vary for each row in the datatable, eh trying with expression language but simply does not work.

这是代码:

<ace:dataTable  value="#{TreeTableBk.rubros}" var="rubro"
                paginator="true" paginatorPosition="bottom" rows="10" style="font-size: 12px; font-family: Tahoma, Verdana, Arial, Sans-Serif">
                <ace:column
                    headerText="Informacion">
                    <div align="left">
                    <h:panelGroup>
                        <c:forEach begin="0" end="#{rubro.nivel}">
                            <h:outputText value="__*"></h:outputText>
                        </c:forEach>
                        <h:inputText 
                        value      = "#{rubro.codigoPadre}"
                        rendered   = "#{rubro.final_ == 'N'}"
                        styleClass = "SMRInputTextNegrilla6" 
                        style      = "background-color:#A5CCE7">
                        </h:inputText>
                    <h:inputText 
                        value      = "#{rubro.codigoPadre}"
                        rendered   = "#{rubro.final_ == 'S'}"
                        styleClass = "SMRInputTextNegrilla6" >
                    </h:inputText>
                    <h:inputText 
                        value      = "#{rubro.codigo}"
                        styleClass = "SMRInputTextNegrilla6" 
                        style      = "background-color:#D1F2C7">
                    </h:inputText>
                    <h:inputText 
                        value      = "#{rubro.descripcion}"
                        styleClass = "SMRInputTextNegrilla6" >
                    </h:inputText>
                    <h:inputText value="#{rubro.nivel}" styleClass="SMRInputTextNegrilla6">
                    </h:inputText>
                    </h:panelGroup>
                    </div>
                </ace:column>
            </ace:dataTable>

知道如何解决这个问题会有所帮助。

非常感谢

【问题讨论】:

    标签: jsf facelets icefaces mojarra


    【解决方案1】:

    &lt;c:forEach&gt; 之类的标记处理程序在构建视图期间运行,而不是在渲染视图期间运行。像 &lt;h:dataTable&gt; 这样的 UI 组件在渲染视图期间运行,而不是在构建视图期间运行。所以,在 &lt;c:forEach&gt; 运行的那一刻,&lt;h:dataTable&gt; 没有运行,因此它的 var="rubro" 在 EL 范围内永远不可用,因此 #{rubro}&lt;c:forEach&gt; 内的 &lt;c:forEach&gt; 总是 评估作为null

    因此,&lt;c:forEach&gt; 对于这个特定的功能要求是完全不可能的。您最好的选择是使用&lt;ui:repeat&gt;。然而,这不支持&lt;c:forEach&gt;beginend 属性。但是,您可以创建一个custom EL function,它会创建一个给定大小的虚拟数组,然后将其提供给&lt;ui:repeat value&gt;

    例如

    <ui:repeat value="#{my:createArray(robro.nivel)}">
        <h:outputText value="__*" />
    </ui:repeat>
    

    public static Object[] createArray(int size) {
        return new Object[size];
    }
    

    JSF 实用程序库OmniFaces 有一个of:createArray() 函数正是用于此目的,另请参见the of:createArray() showcase example

    另见:

    【讨论】:

    • 非常感谢,这就是问题的解决方案。祝福和好运