java JSF 数据表中的多个页脚行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/751860/
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
Multiple Footer Rows in a JSF dataTable
提问by David Waters
I am wanting to know if there is any way to have multiple footer rows in a h:dataTable (or t:datatable) I want to do somthing like this (which does not compile)
我想知道是否有办法在 ah:dataTable(或 t:datatable)中有多个页脚行我想做这样的事情(不编译)
<h:dataTable ... var="row">
<h:column>
<f:facet name="header">
Header
</f:facet>
<h:outputText value="#{row.value}"/>
<f:facet name="footer">
FirstFooter
</f:facet>
<f:facet name="footer">
Second Footer in a new tr
</f:facet>
</h:column>
</h:dataTable>
With the result being somthing like this
结果是这样的
<table border=1 style="border:1px solid">
<thead><tr><th>Header</th></tr></thead>
<tbody><tr><td>Data Value 1</td></tr>
<tr><td>Data Value 2</td></tr>
<tr><td>Data Value ...</td></tr>
<tr><td>Data Value n</td></tr>
</tbody>
<tfoot>
<tr><td>FirstFooter</td></tr>
<tr><td>Second Footer in a new tr</td></tr>
</tfoot>
</table>
Any ideas how best to accomplish this? Thanks.
任何想法如何最好地实现这一目标?谢谢。
EDIT: It would be great if I could avoid using a custom control/custom renderer
编辑:如果我可以避免使用自定义控件/自定义渲染器,那就太好了
采纳答案by David Waters
The solution I ended up using was a custom renderer associated with t:datatable (the tomahawk extension to datatable)
我最终使用的解决方案是与 t:datatable(数据表的 tomahawk 扩展)关联的自定义渲染器
public class HtmlMultiHeadTableRenderer extends HtmlTableRenderer
I only had to override one method
我只需要覆盖一种方法
protected void renderFacet(FacesContext facesContext,
ResponseWriter writer, UIComponent component, boolean header)
with in which I looked for facets with names header, header2, header3 ... headerN (I stop looking as soon as there is one missing) and the same with footer. This has allowed me do do code like
我在其中寻找名称为 header、header2、header3 ... headerN(一旦缺少一个我就停止查找)和页脚相同的方面。这让我可以做类似的代码
<h:dataTable ... var="row">
<h:column>
<f:facet name="header">
Header
</f:facet>
<f:facet name="header2">
A second Tr with th's
</f:facet>
<h:outputText value="#{row.value}"/>
<f:facet name="footer">
FirstFooter
</f:facet>
<f:facet name="footer2">
Second Footer in a new tr
</f:facet>
</h:column>
</h:dataTable>
This took about one day(with some other extensions like allowing colspans based on groups) to code and document,
这花了大约一天的时间(还有一些其他的扩展,比如允许基于组的 colspans)来编码和记录,
回答by Matt McMinn
As McDowell said, panelGrid will do what you're asking without custom controls, unless I'm missing something.
正如 McDowell 所说,除非我遗漏了什么,否则 panelGrid 将在没有自定义控件的情况下完成您的要求。
<f:facet name="footer">
<h:panelGrid columns="1">
<h:outputText value="FirstFooter" />
<h:outputText value="Second Footer in a new tr" />
</h:panelGrid>
</f:facet>
This renders as follows
这呈现如下
<tfoot>
<tr>
<td colspan="3">
<table>
<tbody>
<tr>
<td>FirstFooter</td>
</tr>
<tr>
<td>Second Footer in a new tr</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tfoot>
You can use tags other than outputText in the panelGrid as well to get whatever effect you're looking for (just make sure that you wrap them in a panelGroup or something similar).
您也可以在 panelGrid 中使用 outputText 以外的标签来获得您正在寻找的任何效果(只需确保将它们包装在 panelGroup 或类似的东西中)。
回答by Damo
The Richfaces column has the breakBefore attribute that would allow you to do something like this:
Richfaces 列具有 breakBefore 属性,可让您执行以下操作:
<f:facet name="footer">
<rich:column>
<h:outputText value="First Footer"/>
</rich:column>
<rich:column breakBefore="true">
<h:outputText value="Second Footer"/>
</rich:column>
</f:facet>
Which is what you're after but unfortunately not using the library you've indicated.
这就是您所追求的,但不幸的是没有使用您指定的库。
If you're using Facelets you could always just build up a regular table with ui:repeat.
如果您使用的是 Facelets,您总是可以使用ui:repeat构建一个常规表。
回答by McDowell
(Facets must have unique names (they are stored in a facet map).)
Two ways to do this spring to mind:
记住这个春天的两种方法:
- Just put a panelGridin the footer and use style sheets to get the look you want.
- Create a custom componentthat rendersthe markup you want.
回答by Markus Heberling
I couldn't use a custom renderer, so I did it with plain html inside the JSF:
我无法使用自定义渲染器,所以我在 JSF 中使用纯 html 进行了操作:
<t:dataTable value="#{...}" var="...">
<f:facet name="header">
<h:outputText escape="false" value="</th></tr>"/>
<tr>
<th rowspan="2" class="tableHeaderLightGrey">
Zeit
</th>
<th colspan="#{filterController.trafficClass.size()}" class="tableHeaderLightGrey">
Verbrauch
</th>
<th colspan="#{filterController.trafficClass.size()}" class="tableHeaderLightGrey">
Bandbreite
</th>
<th colspan="#{filterController.trafficClass.size()}" class="tableHeaderLightGrey">
Auslastung
</th>
<th rowspan="2" class="tableHeaderLightGrey">
Gesamtauslastung
</th>
</tr>
<tr>
<c:forEach items="#{filterController.trafficClass}" var="trafficClass">
<th class="tableHeaderLightGrey">
#{trafficClass.name}
</th>
</c:forEach>
<c:forEach items="#{filterController.trafficClass}" var="trafficClass">
<th class="tableHeaderLightGrey">
#{trafficClass.name}
</th>
</c:forEach>
<c:forEach items="#{filterController.trafficClass}" var="trafficClass">
<th class="tableHeaderLightGrey">
#{trafficClass.name}
</th>
</c:forEach>
</tr>
<h:outputText escape="false" value="<tr><th>"/>
</f:facet>
... columns ...
</t:dataTable>
The header facet opens a <tr><th>which I clode right away with the outpuut text and open again at the end to get valid html. You will get an empty <tr><th></th></tr>before and after your custom header that way, but that was acceptable in my case.
标题方面会打开一个<tr><th>我立即使用输出文本进行处理并在最后再次打开以获得有效的 html。这样您<tr><th></th></tr>的自定义标头前后都会有一个空的,但在我的情况下这是可以接受的。

