是否可以检查在2D数组上是否存在特定的元素?
<div th:each="employee, i : ${company.employees}">
<div th:each="duty, j : ${employee.duties}">
<div class="custom-control custom-checkbox my-1 mr-sm-2">
<input type="checkbox" name="duties"
th:id="${j.index}" th:value="${j.index}"
th:checked="${#arrays.contains(${company.tasks[i.index][]}, ${duty.id})}" />
<label th:for="${j.index}" th:text="${j.index}"></label>
</div>
</div>
</div>Java等效项应该是:
for (int i=0; i<company.getEmployees().length; i++)
for (Duty duty : company.getEmployees().[i].getDuties())
boolean checked = company.getTasks()[i].contains(duty.id);
// Contains, method that checks whether element exists on array发布于 2018-08-12 02:38:59
别干那事!
Thymeleaf和其他前端框架不适合数据提取和转换,应该在较低的1到2层执行。这些框架的作用是绑定、发布和显示一个已经转换的模型,这个模型在最好的情况下应该已经是最终形式了。
我建议您在将数据公开给模板之前执行此条件数据提取。在模板中只需要一个迭代。
https://stackoverflow.com/questions/51800928
复制相似问题