2013-02-07 1 views

risposta

9

According the documentation, il g: ogni tag nella vista gsp permette lo "status" variabile dove grails memorizzare l'indice di iterazione Esempio:.

<tbody> 
    <g:each status="i" in="${itemList}" var="item"> 
    <!-- Alternate CSS classes for the rows. --> 
    <tr class="${ (i % 2) == 0 ? 'a' : 'b'}"> 
     <td>${item.id?.encodeAsHTML()}</td> 
     <td>${item.parentId?.encodeAsHTML()}</td> 
     <td>${item.type?.encodeAsHTML()}</td> 
     <td>${item.status?.encodeAsHTML()}</td> 
    </tr> 
    </g:each> 
</tbody> 
+0

che ha funzionato, grazie un milione! – grantmcconnaughey

+0

Questa risposta è stata molto utile in quanto ha sottolineato l'esistenza della funzione "stato" in

2

Qualsiasi di g:each, eachWithIndex o for cicli possono essere usato.

Ma, per questo caso specifico, il valore dell'indice non è necessario. Usando i CSS pseudo-classi si raccomanda:

tr:nth-child(odd) { background: #f7f7f7; } 
tr:nth-child(even) { background: #ffffff; } 

Se è ancora necessario per ottenere l'indice, opzioni sono:

<g:each status="i" in="${items}" var="item"> 
    ... 
</g:each> 

<% items.eachWithIndex { item, i -> %> 
    ... 
<% } %> 

<% for (int i = 0; i < items.size(); i++) { %> 
    <% def item = items[i] %> 
    ... 
<% } %>