Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counters in Loops in Thymeleaf

Tags:

thymeleaf

Is there a way to do a loop in Thymeleaf without a list?

I'd like to essentially convert this snippet to Thymeleaf:

<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
    <form:option value="${year-i}" />
</c:forEach>
</form:select>

-- Update --

I've decided this is along the lines of how I want to do it, but I'm not sure about the springEL syntax:

<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
like image 230
Mouscellaneous Avatar asked Apr 09 '13 14:04

Mouscellaneous


People also ask

How do you break the loop in Thymeleaf?

The best solution would be the put this logic in the controller and put the first product of type 'T' in a separate attribute. If that's not possible, another solution would be to write a Thymeleaf extension (or if using Spring a bean) that does this.

How do I iterate a list of objects in Thymeleaf?

In Thymeleaf, iteration is achieved by using the th:each attribute. One of the interesting things about this attribute is that it will accept and iterate over some different data types, such as: objects implementing java. util.

How do you use each in Thymeleaf?

We will use th:each to iterate through the list of customers and list of addresses for each customer. CustomerController class was defined to handle all GET requests to /customers URI and return a rendered page customers. html as an output (which is our Thymeleaf template located in /resources/templates ).


2 Answers

In case you are still looking for the correct SpEL syntax, here's what worked for me:

<option th:each="i : ${#numbers.sequence( 1, 100)}"
        th:value="${ (new org.joda.time.DateTime()).getYear() - i }"
        th:text="${ (new org.joda.time.DateTime()).getYear() - i }">1</option>

Notice:

  • added th:text to set the option text.
  • used Joda-Time instead as java.util.Date wouldn't give me the desired outcome

Read this discussion on java.util.Date and getYear()

like image 97
cognant Avatar answered Sep 21 '22 17:09

cognant


You can use the special thymleaf iteration variable inside the each block. This special variable name is the name of your element variable concatenate with the keyword 'Stat' (ex: elt -> eltStat) This variable gives you many information related to the iteration.

You can also specify this variable name after your element variable. For example:

<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

More information in the official documentation below:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

like image 23
fliim Avatar answered Sep 21 '22 17:09

fliim