Senthilrajan VS wrote the following on 7/13/2005 10:53 AM:
Hi All,

I'm using html:iterate to display the set of records in the table. Is

there any way to put the alternate color using CSS classes in tag libraries

Since you will probably need this functionality in several places in your application, I suggest you make tag to do it, vs doing the conditional logic everywhere within the application. Using JSP2.0 I made this simple tag file that I'll list below.

In order to use it you simply replace your standard tr with something like:

<c:forEach items="${associates}" var="associate" varStatus="status">
        <tags:AltRowColor status="${status.index}"/>
                <td>.....</td>

You mentioned you were using logic:iterate, which in that case replace ${status.index} with whatever your current count var is (forgot the logic:iterate syntax).

The tag file takes some optional parameters. If you provide nothing like I did above (other than status) it will create class="odd" and class="even" so you'll need to have an odd and even classes definined in a style sheet somewhere. However, if you don't like those terms 'odd' and 'even', you can pass in your own class names (ie odd="myOddClass"). It also lets you pass in an optional class name in case you already have one defined that handles the rest of the row format. (The only thing you might want to add to the tag is the option to take a 'style' definition that would be the custom hardcoded style - I don't like that practice though since typically I like to define classes that have my styles, since it's easier to maintain).

Also, assuming you put this tag file below in a directory under WEB-INF called "tags," don't forgot the import on the top of the pages you want to use like this:

<%@ taglib prefix="tags" tagdir= "/WEB-INF/tags" %>


Here's the simple tag file:

//AltRowColor.tag

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>
<%@ attribute name="status" %>
<%@ attribute name="odd" %>
<%@ attribute name="even" %>
<%@ attribute name="styleClass" %>
<%-- if user doesn't provide an odd or even value it uses odd and even as class names --%>
<c:if test='${even == null || even == "" }'>
    <c:set var="even" value="even"/>
</c:if>
<c:if test='${odd == null || odd == "" }'>
    <c:set var="odd" value="odd"/>
</c:if>
<c:choose>
    <c:when test="${(status +1) % 2 != 0 }">
        <tr class="${odd} ${styleClass}">
    </c:when>
    <c:otherwise>
        <tr class="${even} ${styleClass}">
    </c:otherwise>
</c:choose>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to