Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop in thymeleaf

Tags:

How can I do the following (java):

for(int i = 0; i < 81 ; i+=20){    //Should loop through 5 times! } 

in Thymeleaf?

I've tried this:

<option th:each="i : ${#numbers.sequence( 1, 81/20)}">    <p th:text="${ i }"></p> <!-- THIS loops 4 times, instead of 5 --> </option> 

The problem is that it is not as accurate as the java piece of code. How to do this?

like image 247
user1275645 Avatar asked Dec 17 '13 11:12

user1275645


People also ask

How do I iterate a list 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 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 map in Thymeleaf?

Iterating over a Map in Thymeleaf To iterate over a Map in this case the HashMap implementation of Map, we will use th:each. It is used for iteration over any collection. We can use key and value on the map to fetch the key and value respectively.

What is th block in Thymeleaf?

someHtmlTag> The <th:block> tag is a virtual tag in the Thymeleaf, it does not correspond to any HTML tags, but it is useful in many cases, for example, you can set the th:each attribute in this tag.


2 Answers

Add step to your code is quite easy.

#{numbers.sequence(0, 81, 20)} 
like image 107
windX Avatar answered Sep 27 '22 18:09

windX


use iterStat key word to iterate. Example If you have an Array of String and you are iterating the same using thymeleaf.

<div th:each="str,iterStat : strings">    <span th:text="${str}"/><!--This will print the index value-->    <span th:text="${iterStat.index}"/><!--This will print the Index--> </div>  
like image 26
sitakant Avatar answered Sep 27 '22 19:09

sitakant