Alternating the color of "batches" of HTML table rows

Alternating the color of "batches" of HTML table rows

I'm posting to share the following code that Support gave to me that alternates the row colors of "batches" of rows.

Use the following in an HTML snippet in a page.

(Personally, I prefer to concatenate all of my HTML in an HTML variable then showing that, rather than alternating back and forth from Deluge to HTML).

  1. <%{
  2. lst1 = {"red","green","blue","white","green"};
  3. lst2 = {1,2,3,4,5};
  4. %>
  5. <table border='black 1px' style="width:100%">
  6. <%
  7.         // x is a counter for the outer loop lst1, and will alter clr below when lst1 changes to another value
  8. x = 0;
  9. for each  r1 in lst1
  10. {
  11. x = x + 1;

  12. // the % (modulus) divides the left hand by the right hand and returns a remainder, b % a = 0
  13. // i.e. if x is odd (e.g. 1, 3, 5), there will be a remainder, hence NOT == 0, so paint the row violet
  14. // if x is even (2, 4, 6) there will NOT be a remainder, hence == 0, so paint the row orange
  15. clr = if(x % 2 == 0, "orange", "violet");

  16.                 // the "batches" of rows / records that belong to each element in lst1
  17. for each  r2 in lst2
  18. {
  19. %>
  20.                         <tr style="background-color:<%=clr%>">
  21. <td><%=r1%></td>
  22. <td><%=r2%></td>
  23. </tr>
  24. <%
  25. }
  26. }
  27. %>
  28. </table>
  29. <%

  30. }%>