11

我有一个可编辑<p:dataTable><p:cellEditor>,我想使用<p:dataExporter>.

我已经包含了itext 2.1.7 jar。我得到了 PDF 格式的输出,但它显示了Object#toString()所有<p:cellEditor>组件的值,如下所示:

org.primefaces.component.celleditor.CellEditor@1bd59e1

如何导出的输出值<p:cellEditor>呢?

4

2 回答 2

12

PrimeFaces 标准数据导出器<p:cellEditor>确实无法识别。我之前已将此作为问题 4013报告给 PF 人员,并附有一个示例,该示例不仅提到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);
        }
    }

}

然后,要使用它,以编程方式而不是 via 调用它<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()并仅在操作方法中设置文件名。上面的代码只是示例性的。

于 2013-01-19T11:39:45.027 回答
4

I agree, I also find this approach to customize the Exporter behaviour the most flexible and least painful.

Anyone interested in using the preProcessor/postProcessor methods with this? Here's an example how to do that.

I dared to slightly modify the method from the answer above:

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();

}

And this is how you use it in your page (again, I just modified the above example):

<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}')}" />

Notice that there ARE NO NESTED EL STATEMENTS (that is not allowed anyway), the last two arguments are simple Strings containing EL expressions.

于 2013-04-03T11:15:38.857 回答