Decorating row class and id attributes

ID Email Date Money Click on the link to test
29139 sanctus-nonumy@et.com Thu Jul 08 21:20:21 CEST 2010 5978.0 row id
29888 et-justo@ut.com Tue Sep 07 21:20:21 CEST 2010 6015.0 row id
45395 amet-dolores@amet.com Wed Apr 01 21:20:21 CEST 2009 6114.0 row id
39245 magna-At@et.com Thu Jan 22 21:20:21 CET 2009 4931.0 row id
25124 sed-kasd@Lorem.com Mon May 24 21:20:21 CEST 2010 6986.0 row id
92677 labore-sadipscing@amet.com Sun Jan 03 21:20:21 CET 2010 6658.0 row id
42411 ipsum-no@voluptua.com Sun Jul 18 21:20:21 CEST 2010 3111.0 row id
48750 tempor-consetetur@sea.com Mon May 25 21:20:21 CEST 2009 1948.0 row id
32974 dolore-magna@duo.com Tue Feb 17 21:20:21 CET 2009 5931.0 row id
27831 dolore-invidunt@justo.com Wed Jun 16 21:20:21 CEST 2010 9588.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.