I'm not sure David's solution would work since, afaict, this would output two <tr> tags, wouldn't it?
(There is a startRow() and an endRow() (or finishRow()) method in TableDecorator which you can override, but i've never really understood how it could be used)
g
James Cook wrote:
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: YOU BE THE JUDGE. Be one of 170 Project Admins to receive an Apple iPod Mini FREE for your judgement on who ports your project to Linux PPC the best. Sponsored by IBM. Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php _______________________________________________ displaytag-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/displaytag-user
-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170
Project Admins to receive an Apple iPod Mini FREE for your judgement on
who ports your project to Linux PPC the best. Sponsored by IBM.
Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php
_______________________________________________
displaytag-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/displaytag-user
------------------------------------------------------- This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170 Project Admins to receive an Apple iPod Mini FREE for your judgement on who ports your project to Linux PPC the best. Sponsored by IBM. Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php _______________________________________________ displaytag-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/displaytag-user
------------------------------------------------------- This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170 Project Admins to receive an Apple iPod Mini FREE for your judgement on who ports your project to Linux PPC the best. Sponsored by IBM. Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php _______________________________________________ displaytag-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/displaytag-user

