How do I change color on every second row?

I want every second row to have another background color
For example:
First row (white)
Second row (light grey)
Thrid row (white)
fourth row (light grey)
<%-- begin Main content here --%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<table>
    <tr>
        <td class="col1 headerrow">Status</td>
        <td class="col2 headerrow">Job</td>
        <td class="col3 headerrow">Customer</td>
        <td class="col3 headerrow">Package</td>
        <td class="col3 headerrow">Load</td>
        <td class="col3 headerrow">Last Build</td>
        <td class="col3 headerrow">URL</td>
        <td class="col3 headerrow">Deploy</td>
    </tr>

    <c:forEach var="server" items="${serverList}">
        <tr>
            <td>
               <c:if test="${server.status=='green'}">
                  <img src="img/sheilds/Shield_Green.png" alt="green" width="40" height="40"/>
               </c:if>
               <c:if test="${server.status=='red'}">
                  <img src="img/sheilds/Shield_Red.png" alt="red" width="40" height="40"/>
               </c:if>
               <c:if test="${server.status=='grey'}">
                  <img src="img/sheilds/Shield_Grey.png" alt="grey" width="40" height="40"/>
               </c:if>
            </td>
            <td><a href="jobsetup.html?id=${server.id}">${server.name}</a></td>
            <td>${server.deployment.customer}</td>
            <td>${server.pakke}</td>
            <td><a href="load.html?id=${server.id}">Load ${server.name}</a></td>
            <td>${server.dateForLastChange}</td>
            <td>${server.url}</td>
            <td><a href="buildanddeploy.html?id=${server.id}">Deploy ${server.name}</a></td>
            
        </tr>

    </c:forEach>
</table>


<body bgcolor="#FFFFFF">

Open in new window

MickeysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sreekanthbhargavCommented:
Hi Mickey,
 U can do like this...

boolean color = true;
Ur loop start i.e <c:forEach var="server" items="${serverList}">
   <%
              String colour = null;
            if (color != true)
            {
           colour="bg-row2";
            

            }
            else
            {
           colour="bg-row1";
                
        }
%>
          <tr class="<%=colour%>">
            <td width="9%"   height="21"   class="td-text">
              <div align="center"><%= TestVal %></div>
            </td>
            <td width="15%"  height="21"   class="td-text"> <%= TestVal  %>
            </td>
            <td width="15%"  height="21"   class="td-text"> <%= TestVal %>
            </td>
 </tr>
          <%            color = !(color) ;
                  }
%>
</c:forEach>
0
mukundha_expertCommented:
basically you will need a counter and check for odd values to set a style for that row..

for eg,
<% int i = 0; %>
<c:forEach var="server" items="${serverList}">
<% i = i+1;%>
  <tr <% if ( i % 2 == 0) { %> class="colouredrow" <%}%>
     
</c:forEach>

or maybe there is a better way
0
mukundha_expertCommented:
race .. did't notice..sorry
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rcflawCommented:
Implement a calculation logic of

var resultOfMod =  1 % 2;
If (resultOfMod==0){
         <tr> desire bgcolor </tr>
}
else
{
         <tr> desire different bgcolor </tr>
}
0
kameshtagoreCommented:
Try something like this

      <c:set var="myColor" value="white" />
    <c:forEach var="server" items="${serverList}">           
               <tr bgcolor='<c:out value="${myColor}"' />>
            <td>
               <c:if test="${server.status=='green'}">
                  <img src="img/sheilds/Shield_Green.png" alt="green" width="40" height="40"/>
               </c:if>
               <c:if test="${server.status=='red'}">
                  <img src="img/sheilds/Shield_Red.png" alt="red" width="40" height="40"/>
               </c:if>
               <c:if test="${server.status=='grey'}">
                  <img src="img/sheilds/Shield_Grey.png" alt="grey" width="40" height="40"/>
               </c:if>
            </td>
            <td><a href="jobsetup.html?id=${server.id}">${server.name}</a></td>
            <td>${server.deployment.customer}</td>
            <td>${server.pakke}</td>
            <td><a href="load.html?id=${server.id}">Load ${server.name}</a></td>
            <td>${server.dateForLastChange}</td>
            <td>${server.url}</td>
            <td><a href="buildanddeploy.html?id=${server.id}">Deploy ${server.name}</a></td>
           
        </tr>
        <c:choose>  
              <c:when myColor="white">  
                    <c:set var="myColor" value="light grey" />
              </c:when>  
              <c:otherwise>
                    <c:set var="myColor" value="white" />
              </c:otherwise>
        </c:choose>
    </c:forEach>
0
MickeysAuthor Commented:
Hmm I have tried all your variants but cant get any of them to work.
0
sreekanthbhargavCommented:
U can get Mr.Mickeys.. U need to define the bg-row1, and bg-row2 to ur desired colors in stye sheet file.

.bg-row1 {
      background-color : #FAFAF8;
}

.bg-row2 {
      background-color : #E6F1FB;
}

I am using the same.... it working for me.... in my application.
0
sreekanthbhargavCommented:

Keep the below in style sheet file.... ex. sample.css
.bg-row1 {
      background-color : #FAFAF8;
}

.bg-row2 {
      background-color : #E6F1FB;
}


Here the block u include in ur jsp.


<link rel="stylesheet" href="/webcontentpath/sample.css" type="text/css">
<% boolean color = true; %>
//Ur loop start
<c:forEach var="server" items="${serverList}">
  <%
              String colour = null;
            if (color != true)
            {
           colour="bg-row2";
           

            }
            else
            {
           colour="bg-row1";
               
        }

%>
  <tr class="<%=colour%>">
<!-- Ur Data here ->
  </tr>
          <%            color = !(color) ;
                  }
%>
</c:forEach>


Definetly it will work... Do acknowledge.. ur outputt....




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MickeysAuthor Commented:
Yes I do work. Thx alot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.