DataTable - Dynamic Columns

Columns of datatable can be defined dynamically with p:columns component. Since columns are created on-the-fly it is easy to add/remove columns programmatically. Column template below is a white-space separated values that can be a combination of "model", "manufacturer", "color" and "year".

MODEL MANUFACTURER YEAR
3f787c0e Mercedes 2001
20b23988 Renault 1965
93b56c06 Chrysler 1985
10d32ef8 Ford 1972
44f3ca3b Audi 1989
7800e516 BMW 1984
179f857a Mercedes 1997
6e4f7233 Chrysler 1993
dbc1e138 Ford 1985

Source

<h:form id="form">
                
    <h:panelGrid id="grid" columns="3" style="margin-bottom:10px">
        <h:outputLabel for="template" value="Template:" style="font-weight:bold"/>
        <p:inputText id="template" value="#{tableBean.columnTemplate}" size="50"/>
        <p:commandButton id="btn" update="cars" actionListener="#{tableBean.updateColumns}" value="Update" process="@parent"/>
    </h:panelGrid>

    <p:dataTable id="cars" var="car" value="#{tableBean.carsSmall}" filteredValue="#{tableBean.filteredCars}">                    
        <p:columns value="#{tableBean.columns}" var="column" columnIndexVar="colIndex" 
                    sortBy="#{column.property}" filterBy="#{column.property}">
            <f:facet name="header">
                #{column.header}
            </f:facet>

            #{car[column.property]}
        </p:columns>

    </p:dataTable>

</h:form>