aspose file tools*
The moose likes JSP and the fly likes Coloring Table Rows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Coloring Table Rows" Watch "Coloring Table Rows" New topic
Author

Coloring Table Rows

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
I am using jstl to build a table into my html page. One of the columns contains a priority number. I use javascript to read this number(innerHTML) and turn the rows specific colors. Is there a way to hide the column that I am using for priority. It does not affect anything just looks odd and really no need to display.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Why wait until JavaScript can do it? Sounds like it'd be better handled in the JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
Bear Bibeault wrote:Why wait until JavaScript can do it? Sounds like it'd be better handled in the JSP.


Is there any way you can give some help on the correct coding.

Here is my jstl code There is a tenth array element(last column in table) no header exists for it. It is the coulmn that holds value I need to set row color:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

First things first. You are mixing scriptlets with JSTL. What's up with that? That's a recipe for disaster and just plain messy.

Why do you feel the need to resort to scriptlets? <c:set> will allow you to set and change scoped values. Also <c:forEach> can provide loop counts and indexes.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
Bear Bibeault wrote:First things first. You are mixing scriptlets with JSTL. What's up with that? That's a recipe for disaster and just plain messy.

Why do you feel the need to resort to scriptlets? <c:set> will allow you to set and change scoped values. Also <c:forEach> can provide loop counts and indexes.


Can you elaborate a little on how to change the scriptlet code to achive what I need. I got this example from some else. I have used it a lot and seems to work. But I am always looking for better solutions.
Keerthi Sagar
Greenhorn

Joined: Apr 21, 2007
Posts: 28
Hi Steve,
While iterating if you got the required value in that column, add simply the following code.
========
%>
<script>document.getElementById('<%=y%>').style.display = none;</script>
<%
========
Here what I am doing is, closing the jsp scriptlet and adding a javascript code to hide the row.

Hope this works.

Regards,
Y. Keerthi Sagar.


Thanks and Regards,<br />Y. Keerthi Sagar.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

To me that seems like an extremely round-about way to put a style="display:none" attribute on a TD tag. Why wouldn't you just plain put that attribute on the tag in the JSP?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Keerthi Sagar wrote:Hi Steve,
While iterating if you got the required value in that column, add simply the following code.
========
%>
<script>document.getElementById('<%=y%>').style.display = none;</script>
<%
========
Here what I am doing is, closing the jsp scriptlet and adding a javascript code to hide the row.

Hope this works.

Regards,
Y. Keerthi Sagar.



Why would you use JavaScript to hide it like that? Just add the style attribute to it with display none. DOing it with JavaScript is bad for performance of the initial page load and it just looks fugly.

Eric
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
So now I have got the scriptlet code out of my JSTL code. How do I color the current row I am in based on the value of the last element of my foreach item?

This is what I have so far. in this condition I want to turn the row background red:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Either write the style attribute on the <tr> element, or assign it to a CSS class name that colors it.

Basically, you need to decide what the HTML should look like, and then make the JSP generate that.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
Bear Bibeault wrote:Either write the style attribute on the <tr> element, or assign it to a CSS class name that colors it.



Will I use js to do that or is there another way after the <TR> tag hs been initialized to reference it with in the c:when tag?

Here is what I am attempting but getting an error "Object Required":

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

NO JS! Just do it on the server. You have all the data there at your fingertips.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
Bear Bibeault wrote:NO JS! Just do it on the server. You have all the data there at your fingertips.


Ok how do I get a referance to the current row that my JSTL is processing like I do in the js?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Your problem is obscured by a whole lot of diversions, but I think your question is this:

"I am generating a <tr> tag, and I want to set an attribute on this tag which makes the row be a certain colour, but I don't know what that colour is until after I have generated a whole lot more tags. So how do I set that attribute?"

Well, if it were me, I would do some more work in the part of your code (presumably it's a servlet) which puts information into the request context. Have it put the colour in a place where it's immediately accessible when you generate the TR tag.

Looks to me like you have put a list "twoqueList" in the request. And it looks like each entry in that list is simply a list of, um, let's just say somethings. I would change that so that each entry is a bean which contains two things: the colour of the row and the list of somethings.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1488
    
    1
Paul Clapham wrote:
Well, if it were me, I would do some more work in the part of your code (presumably it's a servlet) which puts information into the request context. Have it put the colour in a place where it's immediately accessible when you generate the TR tag.

Looks to me like you have put a list "twoqueList" in the request. And it looks like each entry in that list is simply a list of, um, let's just say somethings. I would change that so that each entry is a bean which contains two things: the colour of the row and the list of somethings.


This is my servlet code that generates my list of lists. The last element added has the color flag. What would I do here to fix it so the JSP could see the color flag at the time the row is defined(the first forEach loop):

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coloring Table Rows