反复创建< tr> AND< td>在Thymeleaf [英] Iterate creation of <tr> AND <td> in Thymeleaf
问题描述
我有一个看起来像这样的数据结构:
I have a data structure that looks like this:
class Tag {
String text;
long count;
}
我将它存储在一个列表中.我将其用于经典的标签云"构造,该构造将显示单词(文本)在数据库中出现的次数(计数),如下所示:
And I have it stored in a in a List. I use this for a classic "tag cloud" construct, where it will show the number of times (count) that a word (text) appears in my database, like so:
Billing,10
Balance,6
Account,3
Complaint,1
我知道我可以使用迭代器将其动态地放置在表中,如下所示:
I know I can use th iterators to dynamically place this in a table, like so:
<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>
但是,我试图将这些数据显示为文本并在单个单元格中计数,然后动态创建网格结构:
However, I'm trying to display this data as text and count within a single cell, and then dynamically create a grid structure:
(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>
如何使用迭代器生成行和列?
How can I use a th iterator to generate both rows and columns?
推荐答案
您应该使用另一个库将数组分成3组(Apache Commons Collections 4.4具有这种方法),然后可以简单地对其进行迭代.例如:
You should use another library to partition the array into groups of 3 (Apache Commons Collections 4.4 has such a method), then you can simply iterate over them. For example:
<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>
这篇关于反复创建< tr> AND< td>在Thymeleaf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!