Search code examples
grailsgsp

Grails inside a <g:each> who creates rows change class of a <tr>


I have my grails view, and using a api rest I Receive an array of items,that I put in a table,and I want to make the pair rows with tr class="alt"> I want for first for second ,and so on. I was thinking to do that with a or creating a tag,but don´t know really if there is something useful that can use for similar cases. The tr that I want to change In the code is as

<table id="customers">
  <tr>
    <th>Foto</th>
    <th>Nombre</th>
    <th>Vendedor</th>
    <th>Precio</th>
    <th>Estado</th>
    <th>Ciudad</th>
  </tr>
   <g:each in="${results}">
**<tr >**
    <td><img src="${it.thumbnail}"></td>

    <td>${it.title}</td>
    <td>${it.nombre_vendedor}</td>
    <td>${it.price}</td>
    <td>${it.condition}</td>
    <td>${it.address.state_name}</td>

  </tr>
    </g:each>

</table>

Solution

  • <g:each in="${results}" status="ix">
      <tr class="${ix % 2 ? 'light' : 'dark'}">
        <td>...
      </tr>
    </g:each>