在百里叶中迭代创建<tr>和<td> [英] Iterate creation of <tr> AND <td> in Thymeleaf

查看:0
本文介绍了在百里叶中迭代创建<tr>和<td>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据结构:

class Tag {
  String text;
  long count;
}

我把它存储在一个列表中。我将其用于一个经典的"标记云"构造,其中它将显示一个单词(文本)在我的数据库中出现的次数,如下所示:

Billing,10
Balance,6
Account,3
Complaint,1

我知道我可以使用这些迭代器动态地将其放入表中,如下所示:

<tr>
  <td>Billing</td><td>10</td>
</tr>
<tr>
  <td>Balance</td><td>6</td>
</tr>
<tr>
  <td>Account</td><td>3</td>
</tr>
<tr>
  <td>Complaint</td><td>1</td>
</tr>

但是,我尝试在单个单元格内将此数据显示为文本和计数,然后动态创建网格结构:

(max column width is 3):

<tr>
  <td>Billing - 10</td>
  <td>Balance- 6</td>
  <td>Account- 3</td>
</tr>
<tr>
  <td>Complaint - 1</td>
</tr>

如何使用第4个迭代器同时生成行和列?

推荐答案

您应该使用另一个库将数组划分为3个组(ApacheCommons Collections4.4就有这样一个方法),然后您可以简单地迭代它们。例如:

<table th:with="partitions=${T(org.apache.commons.collections4.ListUtils).partition(tags, 3)}">
    <tr th:each="partition: ${partitions}">
        <td th:each="tag: ${partition}"><span th:text="${tag.text}" /> - <span th:text="${tag.count}" /></td>
    </tr>
</table>

这篇关于在百里叶中迭代创建&amp;lt;tr&amp;gt;和&amp;lt;td&amp;gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆