PrimeFaces DataTable with Dynamic Columns

By Arvind Rai, January 31, 2015
On this page, we will learn PrimeFaces 5 datatable with dynamic columns. To create dynamic column, PrimeFaces provides columns tag with different attribute to perform sorting, filter etc. Datatable and columns both tag iterates on managed bean property to produce result with the attribute var and value.. Columns tag will looks as below.
<p:columns value=""  var=""  columnIndexVar=""  sortBy=""  filterBy=""> 

Project Structure in Eclipse

Find the project structure in eclipse.
PrimeFaces 5  DataTable  with Dynamic  Columns  Example

Create UI using p:dataTable and p:columns

To create DataTable with dynamic column, PrimeFaces provides p:columns tag which attributes as given below.
value: Access managed bean property for dynamic columns.
var: After iteration, object for column is assigned.
sortBy: Property to sort the column
filterBy: Property to filter the column.
Find the UI for the demo.
<html lang="en"
      xmlns="" xmlns:h=""
	  xmlns:f="" xmlns:p="">
        <title>PrimeFaces 5  DataTable  with Dynamic  Columns  Example</title>
       <h3>PrimeFaces 5  DataTable  with Dynamic  Columns  Example</h3>
	     <p:dataTable value="#{dataTableView.studentList}" var="student">
		<p:columns value="#{dataTableView.dataTableColumns}" var="column" sortBy="#{student[]}">
		     <f:facet name="header">
		         <h:outputText value="#{column.header}" />
		         <h:outputText value="#{student[]}" />				       

Create Managed Bean for View

Create the managed bean to be used in view.
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "dataTableView")
public class DataTableView implements Serializable {
	private static final long serialVersionUID = 1L;
	private  List<Student> studentList = new ArrayList<Student>();
	private  List<DataTableColumn> dataTableColumns = new ArrayList<DataTableColumn>();
        public void init() {
			//add students
			studentList.add(new Student(111, "Ram", "Ayodhya"));
			studentList.add(new Student(222, "Mahesh", "Varanasi"));
			studentList.add(new Student(333, "Krishna", "Mathura"));
			// prepare dynamic columns
			dataTableColumns.add(new DataTableColumn("ID","id"));
			dataTableColumns.add(new DataTableColumn("Name","name"));
			dataTableColumns.add(new DataTableColumn("Location","location"));
	public List<Student> getStudentList() {
		return studentList;
	public List<DataTableColumn> getDataTableColumns() {
		return dataTableColumns;
Find the bean for student data.
package com.concretepage;
public class Student {
	private Integer id;
	private String name;
	private String location;
	public Student(Integer id, String name, String location){ = id; = name;
		this.location = location;
	public Integer getId() {
		return id;
	public String getName() {
		return name;
	public String getLocation() {
		return location;
Find the bean for column property.
package com.concretepage;
public class DataTableColumn {
    private String header;
    private String property;
    public DataTableColumn(String header, String property) {
        this.header = header; = property;
    public String getHeader() {
        return header;
    public String getProperty() {
        return property;


Deploy the war file in tomcat and access the URL as http://localhost:8080/primefaces5-1/students.xhtml
We will find below webpage.
PrimeFaces 5  DataTable  with Dynamic  Columns  Example

Download Source Code


©2024 | Privacy Policy | Contact Us