2

I'm using thymeleaf and I want to iterate a loop and group by every n items. Right now my code wants to group by every 4 items. After a search in most of the questions, this is my code:

<div th:each="items, iter: ${myList}" th:if="${iter.count} % 4 == 0" th:class="${iter.first}? 'nice-slider-slide active first' : 'nice-slider-slide'">
    <div class="nice-slider-entry" th:each="item, iter: ${items}">
        <span th:text="${item.getName}"></span>
    </div>
</div>

The result is blank and it is printing nothing. But the result I want is:

<div class="nice-slider-slide active first">
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
</div>
<div class="nice-slider-slide">
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
    <div class="nice-slider-entry">
        <span>Name</span>
    </div>
</div>
4
  • 1
    You should define a fragement with arguments which renders a block of entries from the list. The index of the first and the index of the last entry that should be added to the block are defined by the arguments. You need some math to call the fragement correct (loop, number of blocks, indices, etc), but that's doable (#numbers.sequence(..., ...), size(), etc.).
    – Flocke
    Sep 19, 2018 at 11:29
  • @Flocke Would please explain how to do it? I added something like: th:each="itemsRow, iter : ${#numbers.sequence(0, myList.size(), 4)}", what about the next each?
    – Afshin
    Sep 19, 2018 at 13:10
  • I will add a answer later.
    – Flocke
    Sep 19, 2018 at 13:30
  • @Afshin -- try this: stackoverflow.com/questions/39509689/…
    – Metroids
    Sep 19, 2018 at 20:49

1 Answer 1

1

Why not move the logic of the grouping outside view layer? Thymeleaf is not so friendly with data computation. Instead of a simple list to have a list of lists:

List<String> myList;

to be replaced by:

List<List<String>> myList;

Your thymeleaf code will be something like this:

    <div th:each="subList,iter : ${myList}" th:class="${iter.first}? 'nice-slider-slide active first' : 'nice-slider-slide'">
        <div class="nice-slider-entry" th:each="index: ${subList}">
            <span th:text="${index}"></span>
        </div>
    </div>

UPDATE with pure Thymeleaf solution:

<th:block th:with="noEl = 4,totalSize = ${#lists.size(myList)} - 1">
    <th:block th:each=" listIndex: ${#numbers.sequence(0, totalSize, noEl)}">
        <div th:class="${listIndex eq 0 }? 'nice-slider-slide active first' : 'nice-slider-slide'"
             th:with="maxValue = ${ totalSize lt (listIndex + noEl -1) ? totalSize : listIndex + noEl -1}">
            <div class="nice-slider-entry" th:each="index : ${#numbers.sequence(listIndex, maxValue)}">
                <span th:text="${myList[index]}"></span>
            </div>
        </div>
    </th:block>
</th:block>
1
  • Updated with thymeleaf solution. Sep 19, 2018 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.