Thymeleaf Loop Until a Number

我只是一个虾纸丫 提交于 2020-05-12 14:53:35

问题


I make a search and get response from server with Thymeleaf. This holds the number of results:

${response.count}

I want to make an iteration like that:

for (int i = 1; i <= response.count; i++) {
    if (response.page == i) {
        <button class="active">Dummy</button>
    } else {
        <button>Dummy</button>
    }
}

How can I do that? I've tried something like that:

${#numbers.sequence(0, response.count)}

but didn't work.

EDIT: I've tried that but didn't work too:

<button th:each="i: ${#numbers.sequence(0, response.count - 1)}" th:class="${i == response.page} ?: active">Dummy</button>

回答1:


This works for me:

<th:block th:each="i: ${#numbers.sequence(0, response.count - 1)}">
    <button th:if="${response.page == i}" class="active">Dummy</button>
    <button th:unless="${response.page == i}">Dummy</button>
</th:block>


来源:https://stackoverflow.com/questions/40007190/thymeleaf-loop-until-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!