java JSF 2 ui:repeat:将 div 中的每 n 个项目分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10481742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSF 2 ui:repeat: group every n items inside a div
提问by Distortum
Given a collection that I want to arrange on a page like this:
给定一个我想在这样的页面上排列的集合:
<!-- Group 0 -->
<div style="float:left;">
<div><!-- Item 0 --></div>
<div><!-- Item 1 --></div>
<!-- ... -->
<div><! -- Item n - 1 --></div>
</div>
<!-- Group 1 -->
<div style="float:left;">
<div><!-- Item n --></div>
<div><!-- Item n + 1 --></div>
<!-- ... -->
<div><! -- Item 2n - 1 --></div>
</div>
<!-- ... -->
<!-- Group g -->
<div><!-- Item gn --></div>
<div><!-- Item gn + 1 --></div>
<!-- ... -->
<div><! -- Item (g + 1)n - 1 --></div>
</div>
Is there some sort of trick I can use to do this inside a ui:repeat or by some other technique, preferablyother than creating a custom component?
我可以使用某种技巧在 ui:repeat 中或通过其他技术(最好不是创建自定义组件)来执行此操作吗?
回答by BalusC
You can check the current loop round by the varStatus
attribute and print the intermediary </div><div style="float: left;">
whenever necessary.
您可以通过varStatus
属性检查当前循环轮次,并</div><div style="float: left;">
在必要时打印中介。
E.g. every 3 items:
例如每 3 个项目:
<div style="float: left;">
<ui:repeat value="#{bean.list}" var="item" varStatus="loop">
<h:outputText value="</div><div style='float: left;'>" escape="false" rendered="#{not loop.first and loop.index % 3 == 0}" />
<div>#{item}</div>
</ui:repeat>
</div>
Note that it's not possible to wrap this as plain HTML inside a <h:panelGroup>
, because it would result in non-wellformed XML, hence the <h:outputText escape="false">
with them as XML entities.
请注意,不可能将其作为纯 HTML 包装在 a 中<h:panelGroup>
,因为这会导致格式不正确的 XML,因此将<h:outputText escape="false">
它们作为 XML 实体。
Updateas per the comments, here's an alternate approach having the <div>
s definied only once which is probably less confusing:
根据评论更新,这是另一种方法,<div>
只定义 s 一次,这可能不那么令人困惑:
<ui:repeat value="#{bean.list}" var="item" varStatus="loop">
<h:outputText value="<div style='float: left;'>" escape="false" rendered="#{loop.index % 3 == 0}" />
<div>#{item}</div>
<h:outputText value="</div>" escape="false" rendered="#{loop.last or (loop.index + 1) % 3 == 0}" />
</ui:repeat>
回答by mrembisz
If possible I would break collection on the server side:
如果可能的话,我会在服务器端破坏收集:
<ui:repeat value="#{groups}" var="group">
<div style="float:left;">
<ui:repeat value="#{group.items}" var="item">
<div>#{item.content}</div>
</ui:repeat>
</div>
</ui:repeat>
another option could be (haven't tested, not sure about size behaviour in particular):
另一种选择可能是(尚未测试,特别是不确定尺寸行为):
<ui:repeat value="#{items}" var="group" varStatus="status" step="n">
<div style="float:left;">
<ui:repeat value="#{items}" var="item" offset="#{status.index}" size="#{status.index + n}">
<div>#{item.content}</div>
</ui:repeat>
</div>
</ui:repeat>
EDIT: the second version has been replaced
编辑:第二个版本已被替换