我可以使用 Sighlty 从 Java 模型的列表中每两个元素添加 html 组件吗?
Can I add html component every two elements of a list from Java model using Sighlty?
我需要使用 Slightly 迭代模型 Java 列表的每两个元素。
所以我可以每两个元素添加 html 个组件。
你能帮帮我吗?
An additional itemList (respectively <variable>List in case a custom identifier/variable was defined using data-sly-list.) identifier is also available within the scope, with the following members:
- index: zero-based counter (0..length-1);
- count: one-based counter (1..length);
- first: true for the first element being iterated;
- middle: true if element being iterated is neither the first nor the > * last;
- last: true for the last element being iterated;
- odd: true if count is odd;
- even: true if count is even.
因此,在您的情况下,自定义标识符为 listIcon
,您需要测试 listIconList.even
而不是 boxSPID.listIcon.even
。
对于您的代码段,如果您想为每个偶数元素插入一个 row
:
<sly data-sly-test="${boxSPID.listIcon}"
data-sly-list.listIcon="${boxSPID.listIcon}">
${listIconList.count} <!-- You can remove this line, it's just for debug -->
<div class="row" data-sly-test="${listIconList.even}">
<div class="${listIcon.iconValue}">
${listIcon.label}
</div>
</div>
</sly>
如果您想将两个连续的元素组合到同一行中,您可以有条件地添加组 start/end 标签:
<sly data-sly-test="${boxSPID.listIcon}"
data-sly-list.listIcon="${boxSPID.listIcon}">
<sly data-sly-test="${listIconList.odd}"><div class="row"></sly>
<div class="${listIcon.iconValue}">
${listIcon.label}
</div>
<sly data-sly-test="${istIconList.even || listIconList.last}"></div></sly>
</sly>
或跳过一组元素(偶数)并通过索引引用它们以将它们包含在输出中(奇数):
<sly data-sly-test="${boxSPID.listIcon}"
data-sly-list.listIcon="${boxSPID.listIcon}">
<div class="row" data-sly-test="${listIconList.odd}">
<div class="${listIcon.iconValue}">
${listIcon.label}
</div>
<div class="${${boxSPID.listIcon[listIconList.count].iconValue}}" data-sly-test="${boxSPID.listIcon[listIconList.count]}">
${boxSPID.listIcon[listIconList.count].label}
</div>
</div>
</sly>
我需要使用 Slightly 迭代模型 Java 列表的每两个元素。 所以我可以每两个元素添加 html 个组件。
你能帮帮我吗?
An additional itemList (respectively <variable>List in case a custom identifier/variable was defined using data-sly-list.) identifier is also available within the scope, with the following members:
- index: zero-based counter (0..length-1);
- count: one-based counter (1..length);
- first: true for the first element being iterated;
- middle: true if element being iterated is neither the first nor the > * last;
- last: true for the last element being iterated;
- odd: true if count is odd;
- even: true if count is even.
因此,在您的情况下,自定义标识符为 listIcon
,您需要测试 listIconList.even
而不是 boxSPID.listIcon.even
。
对于您的代码段,如果您想为每个偶数元素插入一个 row
:
<sly data-sly-test="${boxSPID.listIcon}"
data-sly-list.listIcon="${boxSPID.listIcon}">
${listIconList.count} <!-- You can remove this line, it's just for debug -->
<div class="row" data-sly-test="${listIconList.even}">
<div class="${listIcon.iconValue}">
${listIcon.label}
</div>
</div>
</sly>
如果您想将两个连续的元素组合到同一行中,您可以有条件地添加组 start/end 标签:
<sly data-sly-test="${boxSPID.listIcon}"
data-sly-list.listIcon="${boxSPID.listIcon}">
<sly data-sly-test="${listIconList.odd}"><div class="row"></sly>
<div class="${listIcon.iconValue}">
${listIcon.label}
</div>
<sly data-sly-test="${istIconList.even || listIconList.last}"></div></sly>
</sly>
或跳过一组元素(偶数)并通过索引引用它们以将它们包含在输出中(奇数):
<sly data-sly-test="${boxSPID.listIcon}"
data-sly-list.listIcon="${boxSPID.listIcon}">
<div class="row" data-sly-test="${listIconList.odd}">
<div class="${listIcon.iconValue}">
${listIcon.label}
</div>
<div class="${${boxSPID.listIcon[listIconList.count].iconValue}}" data-sly-test="${boxSPID.listIcon[listIconList.count]}">
${boxSPID.listIcon[listIconList.count].label}
</div>
</div>
</sly>