p:dataExporter does not recognize p:cellEditor

Deadly 提交于 2019-11-26 09:35:37

问题


I have an editable <p:dataTable> with <p:cellEditor> and I want to export the content of that table into PDF format using <p:dataExporter>.

I have included the itext 2.1.7 jar. I got the output in PDF but it shows the Object#toString() values of all <p:cellEditor> components like so:

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

How do I export the output values of the <p:cellEditor> instead?


回答1:


The <p:cellEditor> is indeed not recognized by the PrimeFaces standard data exporters. I've previously reported this to the PF guys as issue 4013 with an example which not only mentions CellEditor, but also HtmlGraphicImage (we are using images to show boolean states, whose alt we'd like to show in PDF/XML/XLS/CSV reports).

First, create a new class which extends the standard PDFExporter like follows:

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

}

Then, to use it, call it programmatically instead of 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')}" />

With

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

Feel free to find the data table by UIComponent#findComponent() instead and to set the filename in action method only. The above code is just exemplary.




回答2:


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.



来源:https://stackoverflow.com/questions/14411389/pdataexporter-does-not-recognize-pcelleditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!