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?
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.
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.
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.
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.
Add step to your code is quite easy.
#{numbers.sequence(0, 81, 20)}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With