2

I found how to do for loop from 1 to 5, and also how to print a map, but couldn't find both together.

I have the following:

<tr th:each="mapEntry : ${users}">
  <td><a target="_blank" th:href="@{'https://github.com/' + ${mapEntry.key}}"><span th:text="${mapEntry.key}"></span></a></td>
  <td th:each="item : ${mapEntry.value}" th:text="${item}"></td>
</tr>

I am trying to print only first 10 values. How could I do it? Thanks!

Isaac Michaan
  • 353
  • 1
  • 2
  • 11
  • 1
    Does this answer your question? [Thymeleaf - How to loop a list by index](https://stackoverflow.com/questions/38367339/thymeleaf-how-to-loop-a-list-by-index). Specifically, use the [iteration status](https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status) features of Thymeleaf, and check the status using `th:if`. – andrewJames Jun 28 '20 at 14:33
  • 2
    Yes. However, it's a too short answer. He could give an example. Thanks. – Isaac Michaan Jun 28 '20 at 15:03
  • 1
    OK - Understood - see if this helps: For the `th:each` tag, use something like this: `th:each="mapEntry,iterStat : ${users}"`. And then for the inner tags, you can choose to display only those where the counter has not reached the maximum count - something like this: `th:if="${iterStat.index} < 10"`. You add this to the tag you want to conditionally display. – andrewJames Jun 28 '20 at 16:25
  • 2
    Thanks. I think I did th:if="${iterStat.index < 10}" and worked fine. – Isaac Michaan Jun 28 '20 at 19:13

0 Answers0