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
784848a7 Ford 2007
a2bbd24d Renault 1968
a744e609 Chrysler 1997
48de1a5a Chrysler 1966
38251bfb Mercedes 2008
75ba1649 Opel 1983
d9c5d82b BMW 1981
7747a858 BMW 1996
72b7617b Chrysler 1969

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>