Close

PrimeFaces - Creating DataTable Programmatically

[Last Updated: Aug 21, 2017]

This example demonstrates how to create DataTable programmatically.

The view

src/main/webapp/index.xhtml

<p:dataTable var="emp" value="#{employeeBean.employeeList}">
    <p:columns value="#{employeeBean.colList}" var="col">
        <f:facet name="header">
            <h:outputText value="#{col.displayName}" />
        </f:facet>
        <h:outputText value="#{emp[col.propertyName]}" />
    </p:columns>
</p:dataTable>

The managed bean

@ManagedBean
@ViewScoped
public class EmployeeBean {
    private List<ColumnInfo> colList = new ArrayList<>();
    private List<Employee> employeeList = new ArrayList<>();

    @PostConstruct
    private void postConstruct() {
        initColumnProperties();
        initEmployeeList();
    }

    private void initColumnProperties() {
        addColumn("id", "ID");
        addColumn("name", "Name");
        addColumn("phoneNumber", "Phone Number");
        addColumn("address", "Address");
    }

    private void addColumn(String propertyName, String displayName) {
        colList.add(new ColumnInfo(propertyName, displayName));
    }

    private void initEmployeeList() {
        DataFactory dataFactory = new DataFactory();
        for (int i = 1; i < 20; i++) {
            Employee employee = new Employee();
            employee.setId(i);
            employee.setName(dataFactory.getName());
            employee.setPhoneNumber(String.format("%s-%s-%s", dataFactory.getNumberText(3),
                    dataFactory.getNumberText(3),
                    dataFactory.getNumberText(4)));
            employee.setAddress(dataFactory.getAddress() + "," + dataFactory.getCity());
            employeeList.add(employee);
        }
    }

    public List<Employee> getEmployeeList() {
        return employeeList;
    }

    public List<ColumnInfo> getColList() {
        return colList;
    }

    public static class ColumnInfo {
        private String propertyName;
        private String displayName;

        public ColumnInfo(String propertyName, String displayName) {
            this.propertyName = propertyName;
            this.displayName = displayName;
        }

        public String getPropertyName() {
            return propertyName;
        }

        public String getDisplayName() {
            return displayName;
        }
    }
}
public class Employee {
  private long id;
  private String name;
  private String phoneNumber;
  private String address;
    .............
}

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

Output

Example Project

Dependencies and Technologies Used:

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

Programmatic DataTable Columns Creation Select All Download
  • programmatic-data-table-columns
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EmployeeBean.java
          • webapp

    See Also