Decorating row class and id attributes
| ID | Date | Money | Click on the link to test | |
|---|---|---|---|---|
| 22850 | dolores-At@est.com | Sun Jun 03 19:27:23 CEST 2012 | 1047.0 | row id |
| 84091 | clita-voluptua@et.com | Mon Feb 11 19:27:23 CET 2013 | 7452.0 | row id |
| 76270 | sea-et@vero.com | Tue Dec 18 19:27:23 CET 2012 | 1304.0 | row id |
| 54734 | dolore-consetetur@ut.com | Sun Jan 29 19:27:23 CET 2012 | 1866.0 | row id |
| 24740 | no-eirmod@At.com | Thu Jun 30 19:27:23 CEST 2011 | 4815.0 | row id |
| 37555 | rebum-vero@magna.com | Fri Nov 04 19:27:23 CET 2011 | 4226.0 | row id |
| 17620 | elitr-labore@Lorem.com | Mon Jan 14 19:27:23 CET 2013 | 8386.0 | row id |
| 78483 | sadipscing-dolor@ipsum.com | Thu Jan 31 19:27:23 CET 2013 | 5224.0 | row id |
| 38784 | clita-no@et.com | Sun Jan 13 19:27:23 CET 2013 | 3932.0 | row id |
| 60881 | takimata-accusam@dolor.com | Tue Aug 07 19:27:23 CEST 2012 | 7540.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.