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

Sometimes colspan attribute is very usefull.
For programmer that used to read and write HTML tags, table-tr-td is common knowledge with tons of examples and tips.

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.
colspan can be calculated depended on type of row in the table or based on some field in the row
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 TableWithDynamicColspans.jspx
	<table border="1"> 
		<caption>table with dynamic colspans</caption> 
		<ui:repeat value="#{tableWithDynamicColspansBean.rows}" var="row"> 
				<td><ice:outputText value="#{}" /></td> 
				<!-- colspan value here is dynamic --> 
				<td colspan="#{ == null? 2 : 1}"><ice:outputText 
					value="#{}" /></td> 
				<ui:fragment rendered="#{ != null}"> 
					<td><ice:outputText value="#{}" /></td> 

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

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

* Java class for example 'table with dynamic colspans'
* referenced as 'tableWithDynamicColspansBean' from faces-config.xml, 
* used in 'TableWithDynamicColspans.jspx'
public class TableWithDynamicColspansBean{
	List<ManWithPhone> rows = null;
	public List<ManWithPhone> getRows() {
		if (rows == null) {
			rows = new ArrayList<ManWithPhone>();
			// create same sample data...
			rows.add(new ManWithPhone("Gary", "1234-6767", ""));	
			rows.add(new ManWithPhone("Bob", "5678-4553", null));
			rows.add(new ManWithPhone("John", "5698-4333", null));
		return rows;
} // end of class

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

public class ManWithPhone {
	public ManWithPhone(String name, String phone, String email) {
		super(); = name; = phone; = email;

	String name;
	String phone;

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

	public void setName(String name) { = name;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) { = phone;

	public String getEmail() {
		return email;

	public void setEmail(String email) { = email;

part of faces-config.xml


As we saw in this example, you can create perfect table with dynamic colspans 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