• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Coloring Table Rows Based on Array Value

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So where's "status"?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know where we are!
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic