Decorating row class and id attributes

ID Email Date Money Click on the link to test
98490 sanctus-duo@rebum.com Tue Mar 23 13:50:53 CET 2010 7246.0 row id
96495 rebum-sed@gubergren.com Tue Dec 15 13:50:53 CET 2009 8238.0 row id
62554 magna-kasd@et.com Sat Nov 21 13:50:53 CET 2009 8531.0 row id
9650 voluptua-est@accusam.com Wed Oct 06 13:50:53 CEST 2010 437.0 row id
37408 sit-erat@clita.com Sat Mar 27 13:50:53 CET 2010 4257.0 row id
79010 no-diam@sed.com Mon Nov 02 13:50:53 CET 2009 5103.0 row id
88899 diam-duo@clita.com Tue Jul 28 13:50:53 CEST 2009 9506.0 row id
96097 diam-no@ea.com Mon Jun 15 13:50:53 CEST 2009 4155.0 row id
42437 eirmod-diam@justo.com Tue Aug 31 13:50:53 CEST 2010 7894.0 row id
97264 est-diam@diam.com Thu Aug 20 13:50:53 CEST 2009 2112.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.