aspose file tools*
The moose likes JSP and the fly likes Coloring Table Rows Based on Array Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Coloring Table Rows Based on Array Value" Watch "Coloring Table Rows Based on Array Value" New topic
Author

Coloring Table Rows Based on Array Value

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
I am using JSTL to populate a table dynamically from a remote database. A particular element in each array holds the value of status field for that record. 1 = high, 2 = caution, 3 = low. As the table gets populated using the forEach iterator I would like to turn the row red for 1, yellow for 2 and leave as is for 3.

I build the array in a servlet using:

Note: Element(0) is the status field.

The HTML JSTL runs two forEach loops. The first iterates through each record and the second through each field of the record. How can I get the status to be read on the first forEach?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Firstly, I know you've been around long enough to know to use code tags when posting code. please fix that.

But secondly, the code you have shown doesn't help us understand what's being iterated over, or where "status" comes from.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Backing up a bit, the overall strategy should be to use that status value to set a CSS class name on the <tr> element. Then you can use CSS to define whatever rendition you want for those classes.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Bear Bibeault wrote:Backing up a bit, the overall strategy should be to use that status value to set a CSS class name on the <tr> element. Then you can use CSS to define whatever rendition you want for those classes.


Yes, I have thought of that but I need to read the status field value on the line item(first forEach) and not the element iteration(second forEach) don't I?

I have just tried to change the servlet code to:



Then do a split on my first forEach so I can use the first element as status value and second element to populate second forEach but I did not get it to work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67



What on earth are you doing?

Just create beans that represent the values you want and populate the List with them. No need to do anything bizarre like splitting!

What is it that you are putting into the List now?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Bear Bibeault wrote:

What on earth are you doing?

Just create beans that represent the values you want and populate the List with them. No need to do anything bizarre like splitting!

What is it that you are putting into the List now?


This is my list code in the servlet:



This is my JSP code:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

That still gives me no clue as to what's being added to the list.

Whatever they are, it looks like you are serializing them to strings with toString(). Why?

And, it looks like you already have an array for the inner loop, why go through all the bother and hassle (and unneeded complexity) of turning it into a List? (And even so, there are cleaner ways to do it).

You really need to back up a bit and examine some high-level decisions you have made that are making all this unnecessarily complex.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Bear Bibeault wrote:That still gives me no clue as to what's being added to the list.

Whatever they are, it looks like you are serializing them to strings with toString(). Why?

And, it looks like you already have an array for the inner loop, why go through all the bother and hassle (and unneeded complexity) of turning it into a List? (And even so, there are cleaner ways to do it).

You really need to back up a bit and examine some high-level decisions you have made that are making all this unnecessarily complex.


Okay, I have re-done a few things. When I first started trying to develope web apps I was told I needed to create an array of arrays to use in the tables. I have up to this point done as suggested. I thought I had tried to assign my class array to a session attribute before and it failed. I was wrong, thanks. You are right it will save a lot of unnecessary coding.

My class where I build the array I use set and get methods:



This line is the status field:

My new servlet code:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

So where's "status"?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Bear Bibeault wrote:So where's "status"?


The field name that represents status is sopryrty.

However, after you got me straightened out on the unnecessary list code I found out I could reference the arry element of the field in question directly in the JSTL forEach.

It is working just fine.

Thanks for all the help. I am sure I will have more chalenges soon.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

You know where we are!
 
wood burning stoves
 
subject: Coloring Table Rows Based on Array Value