• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Display table rows using JSTL with different colors based on condition

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to display a table using jstl. The columns are id, tie_id, comments.
id is the primary key in table. I want to display rows having same tie_id with same color.
How can I write jstl loop for this?
 
Sheriff
Posts: 67753
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
Assign a CSS class name to the <tr> element for the row that's distinct for each distinct value.

The class name can be used to assign different background colors.
 
namita sasa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each row tie_id may not be different. For example, first three rows will have same tie_id=1, next two will have same tie_id=2 etc.
I need to compare tie_id of each row using jstl.
 
Bear Bibeault
Sheriff
Posts: 67753
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
Comparisons are easy; use the == (or eq) operator.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many different tie_id values might you have? 3? 47? 1023? 1048576?
If you're going to come up with a unique color for each unique tie_id, will you have enough colors?

Are the records going to be grouped by tie_id? If not, should they be?

If they are grouped by tie_id, then can't you just alternate colors between different sets of tie_id values, as in:
 
namita sasa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies Mark and Bear. I don't know in future how many tie_id's will be there. So, I am going to repeat the colors. I have set color array of string in session having 10 colors. After the 10'th color, again the first color repeats and so on.
The records are grouped by tie_id. I have set following loop and getting error message. I am taking value of tie_id of first row=first_tieId and then compare it with tie_id of other records. If tie_id are different, a new color will be set.


Erroe Message : Unable to find a value for "1" in object of class "java.lang.String" using operator "[]"

Please help!
 
namita sasa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting stuck because I am not able to access the color array in JSP which is in session scope.


Help!
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you create this array and place it in the session scope (show the code that did this).
 
namita sasa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the answer. I just accessed it using ${sessionScope.color[i]} and it worked. Thanks again you all!!
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

namita sasa wrote:I got the answer. I just accessed it using ${sessionScope.color[i]} and it worked. Thanks again you all!!


Yes, but then you're setting another variable called color? Won't this be confusing?
Perhaps you could call one color_list?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do this:

You're basically hiding the session-scoped var with a page-scoped var (when you use unqualified access to color). Use a different name, like:
 
namita sasa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I got what you said. I will change the names of page scope and session scope variables.

Thanks.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic