Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping dynamic datatable columns in primefaces 3.5

Say I have a <p:datatable> with 2 static columns and a list of dynamic columns. How can I add a header over the dynamic columns only?
To be more specific, here is how it must look:

-------------------------------------------------------------------------
|               |               |         Header over dynamic columns   |
| StaticColumn1 | StaticColumn2 |----------------------------------------
|               |               | DynamicCol1 | DynamicCol2 | ........  |
-------------------------------------------------------------------------
|               |               |             |             |           |
-------------------------------------------------------------------------
|               |               |             |             |           |
-------------------------------------------------------------------------

I've already tried with <p:columnGroup> but it seems that it doesn't support <p:columns>.
Thanks in advance.

like image 842
vadim Avatar asked Dec 20 '22 18:12

vadim


1 Answers

After reading the Primefaces documentation, I found that this feature is not implemented (at least until 4.0 version) so I came up with a workaround:

<p:dataTable value="#{backedBean.datatableList}" var="datatableRow">
    <p:columnGroup type="header">
        <p:row>
            <p:column rowspan="2" headerText="StaticColumn1"/>
            <p:column rowspan="2" headerText="StaticColumn2"/>
            <p:column colspan="#{backedBean.listOfDynamicColumns.size()}" 
                  headerText="Header over dynamic columns" />
        </p:row>
        <p:row>  
            <c:forEach items="#{backedBean.listOfDynamicColumns}" var="dynamicColumn">
                <p:column headerText="#{dynamicColumn.name}" />
            </c:forEach>
        </p:row>
    </p:columnGroup> 

    <p:column>
        #{datatableRow.value1}
    </p:column>
    <p:column>
        #{datatableRow.value2}
    </p:column>
    <c:forEach items="#{backedBean.listOfDynamicColumns}" var="dynamicColumn">
        <p:column>
            #{datatableRow[dynamicColumn]}
        </p:column>
    </c:forEach>

</p:dataTable>

Note that I used <c:forEach> which is a build-time tag instead of <ui:repeat> which is a render-time tag.

like image 157
vadim Avatar answered Dec 28 '22 07:12

vadim