** Thymleaf循环根据下标取值**
1、后台代码
SeedNew seedNew = new SeedNew();
seedNew.setParentId(oneId);
List<SeedNew> seedNews = *****.selectSeedNewList(seedNew);
List<List<SeedNew>> newList = new ArrayList<>();
if (seedNews != null && !seedNews.isEmpty() && seedNews.size() > 0) {
int toIndex = 2;
for (int i = 0; i < seedNews.size(); i += 2) {
if (i + 2 > seedNews.size()) {
toIndex = seedNews.size() - i;
}
List<SeedNew> li2 = seedNews.subList(i, i + toIndex);
ArrayList<SeedNew> arrayList = new ArrayList<SeedNew>();
arrayList.addAll(li2);
if(arrayList.size()==1){
arrayList.add(new SeedNew());
}
newList.add(arrayList);
}
}
return newList;
li2 :是每两条一分为一组,在thymleaf循环时,直接取。
2、前端代码
th:each=“l ,inStat: ${newList}” :l 不能是关键字,否则报错
<div class="box" th:each="l ,inStat: ${newList}">
<div class="box-max">
<div class="box-lip">
<img class="box-lip-img" th:src="@{${l.get(0)?.newPic}}"/>
<div class="box-lip-tx">
<p class="box-lip-p"><span th:text="${l.get(0)?.newName}"></span></p>
<p class="box-lip-r" th:onclick="getNewValue([[${l.get(0).newId}]])">></p>
</div>
</div>
<div class="box-lib">
<img class="box-lip-img" th:src="@{${l.get(1)?.newPic}}"/>
<div class="box-lip-tx">
<p class="box-lip-p"><span th:text="${l.get(1)?.newName}"></span></p>
<p class="box-lip-r" th:onclick="getNewValue([[${l.get(1).newId}]])">></p>
</div>
</div>
</div>
</div>
效果图 整个图是上下一起加载
|