本文介绍了如何在p:dataTable和p:columnGroup中使用c:forEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一种特殊形式的动态"列分组.但是,如我的其他问题所述,该功能不受支持.建议使用 c:forEach 作为可能的解决方案

I want to use a special form of grouping 'dynamic' columns. This is however not supported as stated in my other question. Using c:forEach was suggested as a possible solution

使用 c:forEach 定义表头和列的正确方法.我的代码之前:

Which is correct way to use c:forEach for defining table header and colums.My code BEFORE:

<p:dataTable id="resultTable" var="result" value="#{myBean.searchResults}" scrollable="true">
    <p:columns value="#{myBean.columns}" var="column" columnIndexVar="colIndex">
        <f:facet name="header" >
            <h:outputText value="#{column.header}"/>
        </f:facet>
        <h:outputText value="#{result[column.property]}"/>
   </p:columns>
</p:dataTable>

和代码转换后(没有用-不显示表格):

and code AFTER transformation (didn't work - table is not displayed):

<p:dataTable id="resultTable" var="result" value="#{myBean.searchResults}" scrollable="true">
    <p:columnGroup type="header">
        <p:row>
            <c:forEach items="#{myBean.columns}" var="column">
                <p:column headerText="#{column.header}"/>
            </c:forEach>
        </p:row>
    </p:columnGroup>
    <c:forEach items="#{myBean.columns}" var="column" >
        <p:column>
            <h:outputText value="#{result[column.property]}"/>
        </p:column>
    </c:forEach>
</p:dataTable>

PrimeFaces版本3.4.1

PrimeFaces version 3.4.1

推荐答案

创建动态列的按书"方式是使用"动态列功能.

The "by the book" way to create dynamic columns would be to use the "Dynamic Columns" feature of the data table.

无论如何,我使用c:forEach做了与您过去尝试做的非常相似的事情.唯一的区别是我没有同时使用p:columnGroup.

Any way, I did something very similar to what you are trying to do in the past, using c:forEach. The only difference was that I was not using p:columnGroup at the same time.

所以,像

<p:dataTable id="resultTable" var="result" value="#{myBean.searchResults}" scrollable="true">
    <c:forEach items="#{myBean.columns}" var="column" >
        <p:column headerText="#{column.header}">
            <h:outputText value="#{result[column.property]}"/>
        </p:column>
    </c:forEach>
</p:dataTable>

应该确实有效.通过这种方式,您可以在p:datatable分析树之前在组件树中添加p:column对象.所以,这就是窍门.

should really work. When you do this way, you add p:column objets in the component tree before the p:datatable analyze the tree. So, this does the trick.

请注意,这可能会在边缘ajax情况下导致问题.因此,如果可以的话,请坚持使用内置的动态列功能.

Beware that this can lead to problems in edge, ajax cases. So, if you can, just stick to the built-in dynamic columns feature.

这篇关于如何在p:dataTable和p:columnGroup中使用c:forEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:49