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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…