html - GSP/JSP for loop - first node 'selected' class -


this gsp problem/niggle i've faced before in jsp , i'm looking cleanest possible solution.

essentially, i'm using loop (<g:each> in gsp) iterate through list of items , output html node each item:

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

all <span> nodes contain css class of item, want first node contain selected class. thus, update code to:

<g:each status="i" var="item" in="items">     <g:if test="${i == 0}">         <span class="item selected">${item}</span>     </g:if>     <g:else>         <span class="item">${item}</span>     </g:else> </g:each> 

this seems complicated approach i'm duplicating lot of code. solution use custom tag library , pass current index it:

<g:each status="i" var="item" in="items">     <span class="item <g:getitemclass index='${i}' />">${item}</span> </g:each> 

the tag library return selected when index equal 0, otherwise won't return @ all. again, adds complexity.

other possible solutions:

  • use index in css class name (very messy)
  • set class name var (). not better custom tag imo.
  • use scriptlets (no way)

any other approaches clean , simple?

thanks!

usually it's a:

<g:each status="i" var="item" in="items">     <span class="item ${i == 0 ? 'selected' : ''}">${item}</span> </g:each> 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -