如何在循环百里香内连接2个值

时间:2019-01-03 06:10:27

标签: loops spring-boot thymeleaf concat

我在连接百里香2个值时遇到一些问题

我执行以下操作:

<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td th:utext="${p.preferencias.get(0).getNombrePref()} +'-'+ ${p.preferencias.get(1).getNombrePref()}">...</td>
<td th:utext="${p.email}">...</td>
</tr>

This is how the page looks like

您会看到这不是动态的,因为如果p.preferencias的大小大于2,将是一个问题,而小于2的情况将是相同的。

这是我尝试过的:

<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td th:each="a , arcd : ${p.preferencias}" th:utext="${p.preferencias.get(arcd.index).getNombrePref()}">...</td>
<td th:utext="${p.email}">...</td>
</tr>

这就是我得到的:

And got this

如您所见,第二个值移到email :(然后我想到了concat的值,然后尝试这个:

<tr th:each="p , in: ${info}">
<td th:utext="${p.nombre}">...</td>
<td th:each="a , arcd : ${p.preferencias}" th:utext="${p.preferencias.get(arcd.index).getNombrePref()} + '-' +${p.preferencias.get(arcd.index).getNombrePref()} " >...</td>
<td th:utext="${p.email}">...</td>
</tr>

这是结果:

Result 3

我不知道如何合并这些值并将其保存在一个单元格中 如何执行循环以获取p.preferencias变量中的所有值?

编辑: 在结果3中发布错误的图片现在是正确的图片

1 个答案:

答案 0 :(得分:0)

最终解决了标签td保持打开的问题,这就是为什么每次迭代中的secon值都填充一个下一个单元格的原因,而使用span标签来解决它并在内部进行迭代

<tr th:each="p , in: ${info}">
    <td th:utext="${p.nombre}">...</td>
    <td>
        <span th:each="a , arcd : ${p.preferencias}" th:utext="${p.preferencias.get(__${arcd.index}__).getNombrePref()} +  (${arcd.size-1 > arcd.index}? ', ':'')"></span>
    </td>
    <td th:utext="${p.email}">...</td>
</tr>

Output