【问题标题】:using insertChildren AND getting child attributes使用 insertChildren 并获取子属性
【发布时间】:2013-03-07 01:01:39
【问题描述】:

我实际上是在尝试创建自定义复合表组件,因为 h:datatable 或 p:datatable 不符合我的需要。然而,它应该像 primefaces 数据表一样使用。 在我找到JSF composite component childrens之后 和 Expose items of a list while iterating within composite component我看到了终点,现在卡住了。

我的xhtml

<h:body>
    <sm:datatable mode="columntoggle" id="mytable" value="#{managerBean.objects}" var="object">
        <sm:column header="KeyHeader" property="key">
            <h:outputText value="#{object.key}"/>    
        </sm:column>
        <sm:column header="ValueHeader" property="value">
            <h:outputText value="#{object.value}"/>    
        </sm:column>
    </sm:datatable>
</h:body>

这是数据表组合

<cc:interface>
    <cc:attribute name="id" />
    <cc:attribute name="mode" />
    <cc:attribute name="var" />
    <cc:attribute name="value" type="java.util.List"/>
</cc:interface>

<cc:implementation>
    <table data-role="table" data-mode="#{cc.attrs.mode}" id="my-table">
        <thead>
            <tr>
                <ui:repeat value="#{component.getCompositeComponentParent(component).children}" var="child">
                    <th>
                        <h:outputText value="#{child.attrs.header}"/>
                    </th>
                </ui:repeat>
            </tr>
        </thead>
        <tbody>
            <ui:repeat value="#{cc.attrs.value}" var="object">
                <tr>
                    <c:forEach items="#{cc.children}" var="child" varStatus="loop">
                        <cc:insertChildren/>
                    </c:forEach>
                </tr>
            </ui:repeat>
        </tbody>
    </table>
</cc:implementation>

这就是列组合

<cc:interface>
    <cc:attribute name="header" />
    <cc:attribute name="property" />
    <cc:facet name="content"/>
</cc:interface>

<cc:implementation>
    <td><cc:insertChildren/></td>
</cc:implementation>

独立工作

tbody 独立工作

像上面那样把它们放在一起,我只能得到 tbody。标题总是空着有什么建议吗? 感谢您提前提供的帮助!

【问题讨论】:

  • &lt;table id="my-table - 绕过 JSF 的 id 命名空间。一般来说:不要这样做——你应该只为 JSF 组件设置 id,而不是基本的 HTML 标记。在这种情况下,在复合组件中,您应该有一个顶级的基本 HTML 标记,但将其 id 设置为#{cc.clientId}。这样update="your_comp" 就可以了。

标签: jsf nested components children composite


【解决方案1】:

这不是错误,它实际上按照 mojarra 中的设计工作。使用 cc:insertChildren 将子项移动到 cc:insertChildren 标记的父标记。这个问题等价于另一个问题并且具有相同的解决方案。请参阅我对this question 的回复。

基本上,您在&lt;cc:insertChildren&gt; 周围放置一个&lt;ui:fragment&gt;,将该片段绑定到FacesComponent bean 中的一个属性。

【讨论】:

    【解决方案2】:

    #{cc.children} 相当于 #{component.getCompositeComponentParent(component).children} 应该都可以 但是由于某些原因在某些版本中无法正常工作,我猜这部分 JSF 还是有一点小错误。

    你试过了吗

     #{cc.getFacets().get('javax.faces.component.COMPOSITE_FACET_NAME').children}
    

    请也参考这个帖子,

    In JSF2, how to know if composite component has children?

    【讨论】:

    • 我刚刚尝试过,但并没有真正奏效。唯一的孩子是实现本身。因此 #{child} 的输出是 com.sun.faces.facelets.component.UIRepeat@7f66ff9c com.sun.faces.facelets.component.UIRepeat@430a14ad
    【解决方案3】:

    我遇到了同样的问题。我的解决方法是使用renderFacet,而不是insertChildren

    迭代我使用过的插入元素:#{cc.facets.yourFacetName.children} 并插入我使用过的元素本身:&lt;composite:renderFacet name="yourFacetName"/&gt;

    【讨论】:

      【解决方案4】:

      我发现在同一组件中使用&lt;composite:insertChildren /&gt; 迭代子几乎是不可能的。至少在 Mojarra 2.2.6 实现中。

      中间方面的方法也不适合我。

      最终,我的解决方案是:

      1. 按原样保留子组件(选项卡)
      2. 从复合中删除所有代码:父(选项卡组)组件的实现
      3. 在父组件对应的faces组件中实现encodeAll()

      这个问题已经有一年多了。如果有更简单的解决方案,请分享。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 2015-08-12
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 2018-03-30
        • 1970-01-01
        相关资源
        最近更新 更多