Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display table rows using JSTL with different colors based on condition

 
namita sasa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparisons are easy; use the == (or eq) operator.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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?
 
Kris Schneider
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ok. I got what you said. I will change the names of page scope and session scope variables.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic