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
30aabf06 BMW 1998
8cceb03a Volkswagen 1961
c12fd7df Volvo 1989
b1b94e3a Audi 1968
f99146ec Ford 1974
1a066c9e Volvo 1977
738fdcd4 Mercedes 1994
f4d9a21d Opel 1977
5e778f55 Ford 2004

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="#{car[column.property]}" filterBy="#{car[column.property]}">
            <f:facet name="header">
                #{column.header}
            </f:facet>

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

    </p:dataTable>

</h:form>