Using Iteration variable in thymeleaf

Hi I am new to thymeleaf and am converting ma old project from jsp to thymeleaf.I am trying to convert a piece of code written in jsp which is :

<logic:iterate id="someForm" name="formName" property="nameList" indexId="i">
<%if (i%2==0)
{
 className="even";
 }
 else
 {
  className="odd";
 }
 %>
//some code here

can anyone help me with converting this code in thymeleaf ??

Upvotes: 5

Views: 9355

Answers (2)

thiagotrss
thiagotrss

Reputation: 149

Just in case your div already has a class and you want to append a new class, use th:classappend:

<div class="col-md-12" th:each="propName,iterStat : ${propNames}" th:classappend="${iterStat.odd}? 'odd' : 'even'">
...

Upvotes: 2

tduchateau
tduchateau

Reputation: 4481

What you're looking for is in the Thymeleaf documentation. Assuming you need to iterate over your collection to display div tags:

<div th:each="propName,iterStat : ${propNames}" th:class="${iterStat.odd}? 'odd' : 'even'">
    ...
</div

Upvotes: 13

Related Questions