how to create table with dynamic headers with jsf, icefaces, java and html

Table headers can be more then one line, it can by dynamic too.

New to Java, JSF, Icefaces? Here is some links to start with:
Why and when use ICEFACES?
First steps with jsf and java

We use simplest HTML tags, together with "ui:repeat" tag, which loops on 'tr' element.
Pros: every programmer, even not-jsf, can understand it. CSS is transparent: what you see is what you get.
Customization is simple too.
Use you favorite HTML editor, or JSF editor together with CSS tools to customize every aspect of this table, its easy!
fragment from TableWithDynamicHeaders.jspx
		<caption>table with dynamic headers</caption> 
		<ui:fragment rendered="#{tableWithDynamicHeadersBean.size > 10}"> 
			<td colspan="2">Too many rows, show only first 10</td> 
		<ui:repeat value="#{tableWithDynamicHeadersBean.rows}" var="row"> 
				<td><ice:outputText value="#{}" /></td> 
				<td><ice:outputText value="#{}" /></td> 

This is main entry point. Its referenced by 'tableWithDynamicHeadersBean' from faces-config.xml
package com.gpost.jsfexamples.tableWithDynamicHeaders;

import java.util.ArrayList;
import java.util.List;

import com.gpost.jsfexamples.tableWithDeleteOnClick.ManWithPhone;

* Java class for example 'table with dynamic headers'
* referenced as 'tableWithDynamicHeadersBean' from faces-config.xml, 
* used in 'TableWithDynamicHeaders.jspx'
public class TableWithDynamicHeadersBean{

	List<ManWithPhone> rows = null;
	public List<ManWithPhone> getRows() {
		if (rows == null) {
			rows = new ArrayList();
			// create same sample data...
			rows.add(new ManWithPhone("Gary", "1234-6767"));
			rows.add(new ManWithPhone("Bob", "5678-4553"));
			rows.add(new ManWithPhone("John", "5698-4333"));
		return rows;

	public int getSize() {
		return getRows().size();

} // end of class

This class defines data in row.
package com.gpost.jsfexamples.tableWithDynamicHeaders;

public class ManWithPhone {
	public ManWithPhone(String name, String phone, String creditCard) {
		super(); = name; = phone;
		this.creditCard = creditCard;

	String name;
	String phone;

	String creditCard;
	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) { = phone;

	public String getCreditCard() {
		return creditCard;

	public void setCreditCard(String creditCard) {
		this.creditCard = creditCard;


part of faces-config.xml


As we saw in this example, you can create perfect table with dynamic headers using simple html/css, then add some binding to your java beans and you get very flexible, easily customizable data table without complexity of 'h:dataTable' component.

No comments:

Post a Comment