Provides the Column table data <td> and table header <th> renderer.
The Column object provide column definitions for the {@link Table} object.
Column Options
The Column class supports a number of rendering options which include:
- {@link #autolink} - the option to automatically render href linksfor email and URL column values
- {@link #attributes} - the CSS style attributes for the table data cell
- {@link #dataClass} - the CSS class for the table data cell
- {@link #dataStyles} - the CSS styles for the table data cell
- {@link #decorator} - the custom column value renderer
- {@link #format} - the MessageFormat pattern renderingthe column value
- {@link #headerClass} - the CSS class for the table header cell
- {@link #headerStyles} - the CSS styles for the table header cell
- {@link #headerTitle} - the table header cell value to render
- {@link #sortable} - the table column sortable property
- {@link #width} - the table cell width property
Format Pattern
The {@link #format} property which specifies {@link MessageFormat} patterna is very useful for formatting column values. For example to render formatted number and date values you simply specify:
Table table = new Table("table"); table.setClass("isi"); Column idColumn = new Column("purchaseId", "ID"); idColumn.setFormat("{0,number,#,###}"); table.addColumn(idColumn); Column priceColumn = new Column("purchasePrice", "Price"); priceColumn.setFormat("{0,number,currency}"); priceColumn.setTextAlign("right"); table.addColumn(priceColumn); Column dateColumn = new Column("purchaseDate", "Date"); dateColumn.setFormat("{0,date,dd MMM yyyy}"); table.addColumn(dateColumn); Column orderIdColumn = new Column("order.id", "Order ID"); table.addColumn(orderIdColumn);
Column Decorators
The support custom column value rendering you can specify a {@link Decorator}class on columns. The decorator
render method is passed the table row object and the page request Context. Using the table row you can access all the column values enabling you to render a compound value composed of multiple column values. For example:
Column column = new Column("email"); column.setDecorator(new Decorator() { public String render(Object row, Context context) { Person person = (Person) row; String email = person.getEmail(); String fullName = person.getFullName(); return "<a href='mailto:" + email + "'>" + fullName + "</a>"; } }); table.addColumn(column);
The
Context parameter of the decorator
render() method enables you to render controls to provide additional functionality. For example:
public class CustomerList extends BorderedPage { private Table table = new Table("table"); private ActionLink viewLink = new ActionLink("view"); public CustomerList() { viewLink.setListener(this, "onViewClick"); viewLink.setLabel("View"); viewLink.setAttribute("title", "View customer details"); table.addControl(viewLink); table.addColumn(new Column("id")); table.addColumn(new Column("name")); table.addColumn(new Column("category")); Column column = new Column("Action"); column.setDecorator(new Decorator() { public String render(Object row, Context context) { Customer customer = (Customer) row; viewLink.setValue("" + customer.getId()); return viewLink.toString(); } }); table.addColumn(column); addControl(table); } public boolean onViewClick() { String path = getContext().getPagePath(Logout.class); setRedirect(path + "?id=" + viewLink.getValue()); return true; } }
Internationalization
Column header titles can be localized using the controls parent messages. If the column header title value is null, the column will attempt to find a localized message in the parent messages using the key:
getName() + ".headerTitle"
If not found then the message will be looked up in the
/click-control.properties file using the same key. If a value still cannot be found then the Column name will be converted into a header title using the method: {@link ClickUtils#toLabel(String)}.
@see Decorator
@see Table