我有一个可编辑的 <p:dataTable>
同 <p:cellEditor>
我想使用该表格的内容导出为PDF格式 <p:dataExporter>
。
我已经包括了 itext 2.1.7 jar。我得到了PDF格式的输出,但它显示了 Object#toString()
所有的价值观 <p:cellEditor>
像这样的组件:
org.primefaces.component.celleditor.CellEditor@1bd59e1
如何导出的输出值 <p:cellEditor>
代替?
该 <p:cellEditor>
确实不被PrimeFaces标准数据输出者所认可。
我以前曾向PF家伙报告此事 问题4013 以一个不仅提到的例子 CellEditor
, 但也 HtmlGraphicImage
(我们使用图像来显示布尔状态,其中 alt
我们想在PDF / XML / XLS / CSV报告中显示)。
首先,创建一个扩展标准的新类 PDFExporter
如下:
public class ExtendedPDFExporter extends PDFExporter {
@Override
protected String exportValue(FacesContext context, UIComponent component) {
if (component instanceof CellEditor) {
return exportValue(context, ((CellEditor) component).getFacet("output"));
}
else if (component instanceof HtmlGraphicImage) {
return (String) component.getAttributes().get("alt");
}
else {
return super.exportValue(context, component);
}
}
}
然后,要使用它,请以编程方式而不是通过它来调用它 <p:dataExporter>
。
<p:dataTable binding="#{table}" editable="true" ...>
<p:column><p:cellEditor>...</p:cellEditor></p:column>
<p:column><p:cellEditor>...</p:cellEditor></p:column>
<p:column><p:cellEditor>...</p:cellEditor></p:column>
<p:column exportable="false"><p:rowEditor /></p:column>
</p:dataTable>
<h:commandLink value="PDF" action="#{bean.exportPDF(table, 'filename')}" />
同
public void exportPDF(DataTable table, String filename) throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
Exporter exporter = new ExtendedPDFExporter();
exporter.export(context, table, filename, false, false, "UTF-8", null, null);
context.responseComplete();
}
随意找到数据表 UIComponent#findComponent()
而是仅在动作方法中设置文件名。上面的代码只是示例。
我同意,我也发现这种方法可以最有效地定制Exporter行为,并且最不痛苦。
任何有兴趣使用的人 预处理/后处理 用这个方法?这是一个如何做到这一点的例子。
我敢于从上面的答案中略微修改方法:
public void exportPDF(DataTable table, String filename,
String preProcessor, String postProcessor) throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
ExpressionFactory factory = context.getApplication().getExpressionFactory();
MethodExpression preProcessorME = factory.createMethodExpression(
context.getELContext(), preProcessor, null, new Class[] {Object.class});
MethodExpression postProcessorME = factory.createMethodExpression(
context.getELContext(), postProcessor, null, new Class[] {Object.class});
Exporter exporter = new ExtendedPDFExporter();
exporter.export(context, table, filename, false, false, "UTF-8",
preProcessorMe, postProcessorME);
context.responseComplete();
}
这就是你在页面中使用它的方式(同样,我刚刚修改了上面的例子):
<p:dataTable binding="#{table}" editable="true" ...>
<p:column><p:cellEditor>...</p:cellEditor></p:column>
<p:column><p:cellEditor>...</p:cellEditor></p:column>
<p:column><p:cellEditor>...</p:cellEditor></p:column>
<p:column exportable="false"><p:rowEditor /></p:column>
</p:dataTable>
<h:commandLink value="PDF" action="#{bean.exportPDF(table, 'filename',
'#{yourBean.preProcessPDF}', '#{yourBean.postProcessPDF}')}" />
请注意 没有嵌套的EL声明 (无论如何都不允许),最后两个参数是包含EL表达式的简单字符串。