The dataExporter
component allows us to export the content of the table into various formats such as XLS, PDF, CSV, and XML. It also supports the exporting of the current data on a page. It also supports only selected data of the table by providing the ability to exclude particular columns and manipulate the exported data with pre- and post-processors.
A basic definition of dataExporter
is given here:
<h:commandLink> <p:graphicImage value="/resources/images/export/pdf.png" /> <p:dataExporter type="pdf" target="countriesTable" fileName="countries" /> </h:commandLink>
The dataExporter
component should be nested in a UICommand
component, such as commandLink
or commandButton
. In the previous definition, target
defines the server-side ID of the table, the data of which will be exported. The type
attribute defines the export type, the values of which could be xls
, pdf
, csv
, or xml
. The fileName
attribute defines the filename of the generated export file; by default, the server-side ID of dataTable
is used as the filename.
The target
attribute must point to a PrimeFaces dataTable
component.
The necessary libraries should be available in the classpath to export to PDF or XLS. See the Setting up and configuring the PrimeFaces library recipe in Chapter 1, Getting Started with PrimeFaces, for details on dependencies.
By default, the export is done for the whole dataset. But, it could also be done for the current page by setting the pageOnly
attribute to true
or for only the selected data by setting the selectionOnly
attribute to true
.
The <p:column>
component provides the exportable
attribute where it defines whether the column should be exported or not.
Character encoding can also be set while exporting by setting the encoding
attribute to the corresponding encoding value. The default encoding is UTF-8
.
The dataExporter
component enables preprocessing and postprocessing on the document for customization, such as adding logos, captions, headers/footers, and so on. Preprocessors are executed before the data is exported, and postprocessors are processed after the data is included in the document. The document object is passed to the processor methods as a Java object so that it can be easily cast to the appropriate class. An example of a preprocessor that adds a footer to page numbers of a PDF document is given here:
<p:dataExporter type="pdf" target="countriesTable" fileName="countries" preProcessor="#{dataExportBean.preProcessPDF}" /> public void preProcessPDF(Object document) { Document pdf = (Document) document; HeaderFooter footer = new HeaderFooter(new Phrase("This is page:"), true); pdf.setFooter(footer); }
Data export is a non-AJAX process, so in order to monitor the status, PrimeFaces provides the client-side method monitorDownload
. The method could be bound to the onclick
event of a command component that wraps the dataExporter
, as seen in the following code snippet:
<h:commandLink onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)"> <p:graphicImage value="/resources/images/export/csv.png" /> <p:dataExporter type="csv" target="countriesTable" fileName="countries" /> </h:commandLink>
While the exporting process occurs, the method will trigger two JavaScript methods, showStatus
and hideStatus
, whose names are passed as parameters. These are two simple methods to show and hide a dialog box component and their implementations, along with the dialog, are given here:
<script type="text/javascript"> function showStatus() { statusDialog.show(); } function hideStatus() { statusDialog.hide(); } </script> <p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false"> <p:graphicImage value="/resources/images/ajax-loader.gif" /> </p:dialog>
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter5/dataExport.jsf
.
3.146.176.145