James,

You're not reading my example.  The decorator allows you to analyze the
contents of the cell and dynamically wrap it in a div or span to 'style' it
appropriately, just as you asked.  I will grant you that my solution didn't
give you direct access to style the <TR> and <TD> tags, but it solved your
problem nonetheless and WITHOUT using javascript or JSTL - simply by adding
a column decorator class to the <display:column> tag.  As my example shows,
the class DOES look at the value to be decorated, tries to turn it into a
number (an Integer in the example), check that it is negative, and, if so,
wrap the text in a span to make it bold red in color to stand out.

I understand from your comment that you don't like it, but next time you
should acknowledge that it gives you the functionality you asked for without
having to recode key Displaytag classes or add Javascript or JSTL 'junk' all
over your page.

Regards,
David

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of James
Cook
Sent: Wednesday, September 29, 2004 10:12 AM
To: [EMAIL PROTECTED]
Subject: RE: [displaytag-user] Selective style for row or cell


Thanks for the alternative, David. But the column decorator doesn't seem to
provide any functionality beyond what the TableDecorator provides. The point
of the thread is to expose the attributes of the <tr> or <td> cells so that
they can be dynamic based on the contents of the cell.


> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:displaytag-user-
> [EMAIL PROTECTED] On Behalf Of David G. Friedman
>
> Use the <display:column> tag with the decorator="someClassName"
> attribute.  I just made it work with this sample display table
> entry:
>
> <display:table name="test">
>       <display:column property="relationship" />
>       <display:column property="name" />
>       <display:column property="relationship"  />
>       <display:column property="age"
>               decorator="com.friedsoftware.decorators.ColExample" />
> </display:table>
>
> Where "test" for me is an ArrayList of POJO's with 3 properties.
> Relationship is a String. Name is a String. Age is an Integer.
>
> HERE is the sample column decorator code:
>
> package com.friedsoftware.decorators;
>
> import org.displaytag.decorator.ColumnDecorator;
> import org.displaytag.exception.DecoratorException;
>
> public class ColExample implements ColumnDecorator {
>
>       public String decorate(Object arg0) throws DecoratorException {
>               String str;
>
>       // This code shows how you could use the same decorator for
>               // Long, Integer, and other classes by using the getClass()
>               // and the equals() method
>               //
>               if (arg0.getClass().equals(Integer.class)) {
>                       if (((Integer) arg0).intValue() > 0) {
>                               str = "<span style=\"color:black\">" + arg0
+
> "</span";
>                       } else {
>                               str = "<span style=\"color:red;font-
> weight:bold;\">" + arg0
>                                               + "</span>";
>                       }
>               } else {
>                       str = arg0.toString();
>               }
>               return (str);
>       }
> }
>
> Next time you might want to skim through the CVS repository or the
> javadocs
> and see if something catches your eye, like the interface for
> ColumnDecorator did for me a few minutes ago.
>
> For quick reference the API JavaDocs are:
> http://displaytag.sourceforge.net/apidocs/index.html
>
> Regards,
> David
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of James
> Cook
> Sent: Friday, September 24, 2004 2:24 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [displaytag-user] Selective style for row or cell
>
>
> David, thanks for proposing another solution to the problem. I'm not sure
> how to begin to implement it for adding to the FAQ however. Does
> displaytag
> expose a RowDecorator or CellDecorator in its current release version? Or
> are you suggesting to override the tag library itself.
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:displaytag-
> user-
> > [EMAIL PROTECTED] On Behalf Of David G. Friedman
> > Sent: Thursday, September 23, 2004 8:57 PM
> > To: [EMAIL PROTECTED]
> > Subject: RE: [displaytag-user] Selective style for row or cell
> >
> > James,
> >
> > Why not use a Decorator and override startRow() to open the "<tr>..."
> your
> > own way depending on the row number?  I've only made one decorator (not
> > lately) so I can't tell you offhand how to do it but there is a row
> object
> > and it does have a method isOddRow() or even getRowNumber().  You would
> > just
> > have to skim through the decorator examples on how to access that
> method.
> >
> > Regards,
> > David
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] Behalf Of James
> > Cook
> > Sent: Wednesday, September 22, 2004 12:54 PM
> > To: [EMAIL PROTECTED]
> > Subject: [displaytag-user] Selective style for row or cell
> >
> >
> > This has been asked many times on the list, but I haven't seen an answer
> > (or
> > one that works anyway). Can we get an answer to this (even if the answer
> > is
> > "is not currently supported") and post it to the FAQ? I'll start it out
> > with
> > the question, and some answers that are suboptimal.
> >
> > ------------------
> >
> > How can a user of displaytag control the value of the current cell's or
> > current row's class attribute in a dynamic manner? For example, how can
> > the
> > background of a cell be formatted red if the content value of the cell
> is
> > less than 0?
> >
> > Here is an example of how the JSP source may look without any formatting
> > in
> > place.
> >
> > <display:table name="totals" id="row">
> >     <display:column title="Amount">
> >             ${row.amount}
> >     </display:column>
> > </display:table>
> >
> > For the purposes of this FAQ entry, assume there is a CSS style declared
> > as:
> >     .negative {background-color: red;}
> >
> > Part I - Work with Cell Contents
> > ================================
> > One (less than optimal) solution is to try and wrap the content of the
> > table
> > cell in a block element and apply the 'negative' style when appropriate
> to
> > this block tag.
> >
> > <display:table name="totals" id="row">
> >     <display:column title="Amount">
> >             <c:set var="classStyle" value="" />
> >             <c:if test="${row.amount < 0}">
> >                     <c:set var="classStyle" value="negative" />
> >             </c:if>
> >             <div style="height: 100%;" class="${classStyle}">
> >                     ${row.amount}
> >             </div>
> >     </display:column>
> > </display:table>
> >
> > This approach however requires a few concessions:
> >
> > 1. The <div> inside the cell does not stretch to the borders of each
> cell
> > unless the cellpadding is set to 0.
> > 2. The height: 100% style for the <div> tag does not appear to work in
> IE.
> > (Works in Firefox.)
> > 3. To simulate a change to the background color of the entire row is
> > cumbersome.
> >
> > Part II - Javascript Postprocessing
> > ====================================
> > DisplayTag certainly gives us access to the class attribute of a table
> > cell,
> > by using the following syntax:
> >
> > <display:table name="totals" id="row">
> >     <display:column class="<Insert class here>" title="Amount">
> >             ${row.amount}
> >     </display:column>
> > </display:table>
> >
> > Unfortunately the class we assign to the column's table cell is reused
> for
> > every row in the table. This can be addressed by some postprocessing
> work
> > in
> > ECMAScript.
> >
> > <display:table name="totals" id="row">
> >     <display:column title="Amount">
> >             <c:set var="classStyle" value="" />
> >             <c:if test="${row.amount < 0}">
> >                     <c:set var="classStyle" value="negative" />
> >             </c:if>
> >             <div classHolder="${classStyle}">${row.amount}</div>
> >     </display:column>
> > </display:table>
> >
> > <script type="text/javascript">
> > //<![CDATA[
> > // This script block uses the x* libraries for excellent cross-browser
> > support.
> >
> > // X v3.14.1, Cross-Browser DHTML Library from Cross-Browser.com
> > // Copyright (c) 2002,2003 Michael Foster ([EMAIL PROTECTED])
> > // This library is distributed under the terms of the LGPL (gnu.org)
> >
> > function updateCSS(){
> >     var divs = xGetElementsByAttributeName('classHolder');
> >     for(var i=0; i < divs.length; i++) {
> >             var classHolder = divs[i].getAttribute('classHolder');
> >             if (classHolder) {
> >                     var tdCell = xParent(divs[i], true);
> >                     tdCell.className = classHolder;
> >             }
> >     }
> > }
> > xAddEventListener(window, 'load', updateCSS, false);
> > //]]>
> > </script>
> >
> > This approach uses a very similar JSP syntax as the previous example,
> > however instead of setting the class attribute on the block tag, it uses
> a
> > placeholder attribute called 'classHolder' to hold the indended style
> for
> > the table cell. Once the page has loaded, the script seeks out all of
> the
> > elements that have this attribute set and copies the node.classHolder
> > value
> > to the node.parent.class attribute.
> >
> > This approach however requires a few concessions:
> >
> > 1. Somewhat cumbersome in that it requires an additional (useless) block
> > tag
> > in the content area of the cell just to hold a property.
> > 2. Requires scripting which is not always desirable.
> > 3. Not totally intuitive for someone new to scripting and manipulating
> the
> > DOM.
> >
> > Part III - Other Approaches
> > ===========================
> > These are the two approaches that I have used at times. If you have some
> > other approach, especially if it is simpler with fewer concessions,
> please
> > tack them onto this post. Perhaps, we can get this added to the FAQ at
> > some
> > point.
> >
> > --------------------------
> >
> > Even better, perhaps the authors will expose the cell and row in a more
> > direct manner. At the very least, the following would be pretty useful:
> >
> > <display:table name="totals" id="row">
> >     <display:column title="Amount">
> >             <c:if test="${row.amount < 0}">
> >                     <display:setProperty
> >                             name="table.row.cell.class"
> >                             value="negative" />
> >             </c:if>
> >             ${row.amount}
> >     </display:column>
> > </display:table>



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
displaytag-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-user



-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
displaytag-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-user

Reply via email to