gpt4 book ai didi

java - 在 FreeMarker 中对表列使用多个序列的最佳方法

转载 作者:行者123 更新时间:2023-12-02 09:07:52 25 4
gpt4 key购买 nike

我有两个单独的数据列表(集),我想用它们来创建表格

ColumnA      ColumnB
Value1 Value1
Value2 Value2
Value3 Value3
Value4 Value4
Value5
Value6

我知道我可以通过 POJO 来做到这一点,其中我的集合将是 POJO(例如 a 和 b)字段,我可以在模板中调用它们,例如:

<table>
<tr>
<th>ColumnA</th>
<th>ColumnB</th>
</tr>
<tr>
<#list object as c>
<td>${c.a}</td>
<td>${c.b}</td>
</#list>
</tr>
</table>

但是我想使用单独的集合并分别填充 ColumnA 和 ColumnsB

您能建议最好的方法吗?

例如:

data.put("set1", SET1)

data.put("set2", SET2)

模板:

<table>
<tr>
<th>ColumnA</th>
<th>ColumnB</th>
</tr>
<#list set1 as c>
<tr><td>${c}</td></tr>
</#list>
<#list set2 as d>
<tr><td>${d}</td></tr>
</#list>
</table>

因此它将合并 ColumnA 的两个集合

最佳答案

参见https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_var_sequence

Retrieving data from a sequence

This is the same as for hashes, but you can use the square bracket syntax only, and the expression in the brackets must evaluate to a number, not a string. For example to get the name of the first animal of the example data-model (remember that the number of the first item is 0, not 1): animals[0].name

和内置序列大小:https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_size

size

The number of sub variables in sequence (as a numerical value). The highest possible index in sequence s is s?size - 1 (since the index of the first subvariable is 0) assuming that the sequence has at least one subvariable.

如果你想找到最大的尺寸,请使用 https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_min_max

min, max

Returns the smaller (min) or greatest (max) item of the sequence (or collection). The items must be either all numbers, or all date/time values of the same kind (date-only, time-only, date-time), or else a comparison error will occur

这允许您执行以下操作:

<#list 0..<[sequenceA?size, sequenceB?size]?max as i>
<tr>
<td>${sequenceA[i]!""}</td>
<td>${sequenceB[i]!""}</td>
</tr>
</#list>

另请参阅:

关于java - 在 FreeMarker 中对表列使用多个序列的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59664066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com