0

I have trouble applying this JSTL counter method to my specific case. Maybe there is another better way.

I have this table example:

I have an outer loop, so the c:if conditions gets checked multiple times! And then there are these extra rows I want to add if condition is true:

 <c:forEach>
     <tr...>
     ...
     </tr>

     <c:if test="$("something true here")>
       <c:forEach var="entry" items="${entries}">
           <tr class="row {CHANGE_THIS!}">
           ...
           </tr>
       </c:forEach>
    </c:if>
 </c:forEach>

I want to be able to change the "CHANGE_THIS" class each time I enter back inside the if clause from the outer loop. So each entry inside the loop still gets same tr class values, but when I come back to if clause again, it would only then change the class name: "CHANGE_THIS".

Like so:

1st forEach loop run:

<tr class="row row1">
<tr class="row row1">
<tr class="row row1">

2nd forEach loop run:

<tr class="row row2">
<tr class="row row2">
<tr class="row row2">

JSTL counter would make these entries which I don't want

<tr class="row row1">
<tr class="row row2">
<tr class="row row3">

Also another problem for me is that JSTL counter is based on some size, I would want to have the increase number functionality without a limit. Is there any other way to achieve these entries with increasing number and how could I create new entries of class names row + (number)?

papasvenska
  • 21
  • 1
  • 7
  • Did you try putting the counter on the outer forEach? – rickz Aug 06 '22 at 00:24
  • It is a possibility, but the counter is also based on a given list size to my knowledge? I was wondering if there is a possibility to inject a script between the lines or inject some JSTL for calling out some function which would return my className(number) with increased number +1 each time, which would run without an indexing limit. – papasvenska Aug 06 '22 at 07:39
  • You could create a custom EL function. – rickz Aug 06 '22 at 23:21
  • Maybe a JSP scriptlet? – papasvenska Aug 08 '22 at 08:55
  • Custom EL functions are very easy to use. Anyway, See https://stackoverflow.com/questions/38340291/jsp-what-is-wrong-with-scriptlets-and-what-to-use-instead – rickz Aug 08 '22 at 14:05

0 Answers0