Fahad
Fahad

Reputation: 403

Dynamically change table background color based on value

I have table which is being populted from database and I need to apply background color for each row which is also coming from dtabase. my code is below Im not sure what Im doing wrong but its not working. HELP!

enter image description here

       <tbody> 
  <tr> 
  <c:forEach items="${summary}" var="summary" 
  style='background-color:<c:out value="${summary.color}"></c:out>;'>


 <tr>
 <td><c:out value="${summary.eventDesc}"  /></td>
  <td><c:out value="${summary.labelNbr}" /></td>
 <td><c:out value="${summary.origin}" /></td>
 <td><c:out value="${summary.senderName}" /></td>
 <td><c:out value="${summary.receiverName}" /></td>
 <td><c:out value="${summary.receiptDate}" /></td>
 <td><c:out value="${summary.loadDate}" /></td>
 <td><c:out value="${summary.forecastIsc}" /></td>
 <td><c:out value="${summary.actualIsc}" /></td>
 <td><c:out value="${summary.country}" /></td>
 <td><c:out value="${summary.source}" /></td>
 <td><input type="checkbox" value=""></td>
 </tr>
 </c:forEach>

Upvotes: 0

Views: 6980

Answers (3)

Alex
Alex

Reputation: 11579

Try this (it will reduce number of lines in your code):

<c:forEach items="${summary}" var="summary">
<tr style='background-color: ${summary.color}'>
<td></td>
<td></td>
<td></td>
</tr>
</c:forEach>

Upvotes: 0

Fahad
Fahad

Reputation: 403

Ok so I finally figured it out. Since Im pulling the color from database table following code works fine:

<td bgcolor='<c:out value="${summary.color}"></c:out>'>
    <c:out value="${summary.eventDesc}" />
</td>

Upvotes: 1

Dogoku
Dogoku

Reputation: 4675

if you really have to resort to using the colors from the database, then you have to add an inline style to each <tr> or <td>


I would suggest adding a css class to your <tr> element for each color and then write your css rules like so

tr.class-1 > td { background-color: yellow }

tr.class-2 > td { background-color: red }

tr.class-3 > td { background-color: green }

Upvotes: 0

Related Questions