Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Thymeleaf循环并按每n项分组_Java_Spring_Thymeleaf - Fatal编程技术网

Java Thymeleaf循环并按每n项分组

Java Thymeleaf循环并按每n项分组,java,spring,thymeleaf,Java,Spring,Thymeleaf,我使用的是thymeleaf,我想迭代一个循环,并按每n项分组。现在,我的代码希望按每4项分组。在搜索了大多数问题后,这是我的代码: <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-sl

我使用的是thymeleaf,我想迭代一个循环,并按每n项分组。现在,我的代码希望按每4项分组。在搜索了大多数问题后,这是我的代码:

<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>

结果为空,不打印任何内容。但我想要的结果是:

<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>

名称
名称
名称
名称
名称
名称
名称
名称

为什么不将分组逻辑移到视图层之外?Thymeleaf对数据计算不太友好。 不是简单的列表,而是列表列表:

List<String> myList;
列出myList;
替换为:

List<List<String>> myList;
列出myList;
您的thymeleaf代码如下所示:

    <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>

使用纯Thymeleaf解决方案更新:

<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>


您应该使用参数定义一个fragement,该参数呈现列表中的一个条目块。应该添加到块中的第一个条目的索引和最后一个条目的索引由参数定义。您需要一些数学来调用正确的片段(循环、块数、索引等),但这是可行的(#numbers.sequence(…,…),size(),等等)。@Flocke请解释一下如何做?我添加了如下内容:
th:each=“itemsRow,iter:${#numbers.sequence(0,myList.size(),4)}”
,下一个呢?我稍后会添加一个答案。@Afshin--试试这个:用thymeleaf解决方案更新。