Decorating row class and id attributes

ID Email Date Money Click on the link to test
70900 aliquyam-duo@ea.com Fri Sep 04 22:26:28 CEST 2009 6498.0 row id
15333 vero-ut@sanctus.com Sat Jun 05 22:26:28 CEST 2010 5218.0 row id
69944 sadipscing-sed@magna.com Wed Jul 08 22:26:28 CEST 2009 4100.0 row id
23758 clita-nonumy@sea.com Mon Jan 18 22:26:28 CET 2010 2190.0 row id
99146 eirmod-aliquyam@ipsum.com Tue Jul 21 22:26:28 CEST 2009 5026.0 row id
18886 justo-elitr@eos.com Mon Aug 02 22:26:28 CEST 2010 6981.0 row id
55971 elitr-accusam@sit.com Fri Mar 20 22:26:28 CET 2009 9024.0 row id
59799 takimata-voluptua@gubergren.com Mon Dec 08 22:26:28 CET 2008 9589.0 row id
76309 gubergren-dolor@vero.com Sat Mar 27 22:26:28 CET 2010 8441.0 row id
58663 et-et@justo.com Fri Oct 15 22:26:28 CEST 2010 9045.0 row id


This quick example shows 2 new features in Displaytag 1.1:

  • Changing the class and id attributes for a table row using a table decorator
  • Implementing a simple table decorator on the fly

Decoration of class and id attributes can be done by simply implementing the addRowClass()code> or addRowId() methods.


For this example row that have a money value less than 4.000 $ have been assigned the "bad" css class, other rows have a "good" css class attribute.

The implementation for the addRowClass() method is:

  return ((ListObject)getCurrentRowObject()).getMoney() > 4000 ? "good" : "bad";
  

or, using the new evaluate() utility method in the TableDecorator class:

  return ((Double)evaluate("money")).doubleValue() > 4000 ? "good" : "bad";
  

Combining a static css class added to the column class and a dinamic class added using a table decorator to a whole row, you can easily add different styles also to single cells, without the need for additional attributes. In this exaple cells in the "money" column have a different style when the value is < 4000


The id attribute for the row is generated using the "id" property in the iterated object, plus a "myrow" prefix. The implementation of the addRowId() method is easy:

   return "myrow" + evaluate("id");
  

If you look at the source code you will see that there is no reference to an external decorator; a new decorator is implemented on the fly, extending only the needed methods and placing it into the page context.

Displaytag will look in the page, request, session or attribute scopes for a decorator keyed by the value specified in the decorator table attribute. Only if an object is not found the name of the decorator will be considered as a class name and loaded using reflection.

This behavior will allow you to generate quick decorators directly in the jsp page, passing parameters to existing decorator instances, or storing you set of decorators into request or application scope for configuration and reuse.