How to use c:forEach with p:dataTable and p:columnGroup?

断了今生、忘了曾经 提交于 2020-01-06 04:21:07

问题


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

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 version 3.4.1


回答1:


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

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.

So, code like

<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>

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.

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



来源:https://stackoverflow.com/questions/31605610/how-to-use-cforeach-with-pdatatable-and-pcolumngroup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!