August 12 2015

Jsf datatable row index

Jsf datatable row index

Jsf datatable row index is used to display the row numbers for each row. This can be done by using binding attribute of datatable. Bind the table to the view itself instead of a bean.

<h:dataTable binding=”#{student}”  .. >

Add a column with the following code


#{student.rowIndex + 1}




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""

 <title>Jsf datatable row index</title>


<h2> Jsf datatable row index </h2>


<h:dataTable value="#{studentManagedBean.studentList}" var="stud"
 border="1" binding="#{student}">

 <f:facet name="header">Index</f:facet>
 <h:outputText value="#{student.rowIndex + 1}" />

 <f:facet name="header">Student ID</f:facet>
 <h:outputText value="#{stud.studId}" />

 <f:facet name="header">Name</f:facet>
 <h:outputText value="#{}" />

 <f:facet name="header">Age</f:facet>
 <h:outputText value="#{stud.age}" />




package com.answersz;

import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "studentManagedBean")
public class StudentManagedBean {

private ArrayList<Student> studentList = new ArrayList<Student>();

 public void init() {
 studentList.add(new Student("2015-01", "ERIC", 20));
 studentList.add(new Student("2015-02", "ANDREW", 18));
 studentList.add(new Student("2015-03", "HARRY", 17));

public ArrayList<Student> getStudentList() {
 return studentList;

public void setStudentList(ArrayList<Student> studentList) {
 this.studentList = studentList;


package com.answersz;

public class Student {

 private String studId;
 private String name;
 private Integer age;

 public Student(String studId, String name, Integer age) {
 this.studId = studId; = name;
 this.age = age;

 // getters and setters


Copyright © 2014. All rights reserved.

Posted August 12, 2015 by answersz in category Jsf

Leave a Comment

Your email address will not be published. Required fields are marked *