Lazy loading is fully supported when data is being exported.
<div class="card">
<h:form>
<div class="p-d-flex p-jc-between">
<div>
<h5>Standard</h5>
<p:commandButton value="XLS" styleClass="p-mr-2 p-mb-2">
<p:dataExporter type="xls" target="tbl" fileName="customers"/>
</p:commandButton>
<p:commandButton value="XLXS" styleClass="p-mr-2 p-mb-2">
<p:dataExporter type="xlsxstream" target="tbl" fileName="customers"/>
</p:commandButton>
<p:commandButton value="PDF" styleClass="p-mr-2 p-mb-2">
<p:dataExporter type="pdf" target="tbl" fileName="customers"/>
</p:commandButton>
<p:commandButton value="CSV" styleClass="p-mr-2 p-mb-2">
<p:dataExporter type="csv" target="tbl" fileName="customers" />
</p:commandButton>
<p:commandButton value="XML" styleClass="p-mr-2 p-mb-2">
<p:dataExporter type="xml" target="tbl" fileName="customers"/>
</p:commandButton>
<p:commandButton value="TXT" styleClass="p-mr-2 p-mb-2">
<p:dataExporter type="txt" target="tbl" fileName="customers" exporter="#{dataExporterView.textExporter}"/>
</p:commandButton>
</div>
<div>
<h5>Page Only</h5>
<p:commandButton value="XLS" styleClass="p-mr-2 p-mb-2 ui-button-outlined">
<p:dataExporter type="xls" target="tbl" fileName="customers" pageOnly="true"/>
</p:commandButton>
<p:commandButton value="XLXS" styleClass="p-mr-2 p-mb-2 ui-button-outlined">
<p:dataExporter type="xlsxstream" target="tbl" fileName="customers" pageOnly="true"/>
</p:commandButton>
<p:commandButton value="PDF" styleClass="p-mr-2 p-mb-2 ui-button-outlined">
<p:dataExporter type="pdf" target="tbl" fileName="customers" pageOnly="true"/>
</p:commandButton>
<p:commandButton value="CSV" styleClass="p-mr-2 p-mb-2 ui-button-outlined">
<p:dataExporter type="csv" target="tbl" fileName="customers" pageOnly="true"/>
</p:commandButton>
<p:commandButton value="XML" styleClass="p-mr-2 p-mb-2 ui-button-outlined">
<p:dataExporter type="xml" target="tbl" fileName="customers" pageOnly="true"/>
</p:commandButton>
<p:commandButton value="TXT" styleClass="p-mr-2 p-mb-2 ui-button-outlined">
<p:dataExporter type="txt" target="tbl" fileName="customers" pageOnly="true"
exporter="#{dataExporterView.textExporter}"/>
</p:commandButton>
</div>
</div>
<p:dataTable id="tbl" var="customer" value="#{deLazyView.lazyModel}"
sortMode="single" filteredValue="#{deLazyView.filteredCustomers}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {Exporters}"
paginator="true" rows="10" styleClass="p-mt-2">
<p:column headerText="Name" sortBy="#{customer.name}" filterBy="#{customer.name}">
<h:outputText value="#{customer.name}"/>
</p:column>
<p:column headerText="Country" sortBy="#{customer.country}" filterBy="#{customer.country}">
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.country.name}"/>
</p:column>
<p:column headerText="Company" sortBy="#{customer.company}" filterBy="#{customer.company}">
<h:outputText value="#{customer.company}"/>
</p:column>
<p:column headerText="Representative" sortBy="#{customer.representative}"
filterBy="#{customer.representative}">
<p:graphicImage name="images/avatar/#{customer.representative.image}" library="demo"
width="32" style="vertical-align: middle"/>
<h:outputText style="vertical-align: middle; margin-left: .5rem"
value="#{customer.representative.name}"/>
</p:column>
</p:dataTable>
</h:form>
</div>
@Named
@ViewScoped
public class DataExporterView implements Serializable {
private List<Product> products;
private Exporter<DataTable> textExporter;
@Inject
private ProductService service;
@PostConstruct
public void init() {
products = service.getProducts(100);
textExporter = new TextExporter();
}
public List<Product> getProducts() {
return products;
}
public void setService(ProductService service) {
this.service = service;
}
public Exporter<DataTable> getTextExporter() {
return textExporter;
}
public void setTextExporter(Exporter<DataTable> textExporter) {
this.textExporter = textExporter;
}
}
@Named
@ApplicationScoped
public class ProductService {
List<Product> products;
@PostConstruct
public void init() {
products = new ArrayList<>();
products.add(new Product(1000, "f230fh0g3", "Bamboo Watch", "Product Description", "bamboo-watch.jpg", 65, "Accessories", 24, InventoryStatus.INSTOCK, 5));
products.add(new Product(1001, "nvklal433", "Black Watch", "Product Description", "black-watch.jpg", 72, "Accessories", 61, InventoryStatus.INSTOCK, 4));
products.add(new Product(1002, "zz21cz3c1", "Blue Band", "Product Description", "blue-band.jpg", 79, "Fitness", 2, InventoryStatus.LOWSTOCK, 3));
products.add(new Product(1003, "244wgerg2", "Blue T-Shirt", "Product Description", "blue-t-shirt.jpg", 29, "Clothing", 25, InventoryStatus.INSTOCK, 5));
products.add(new Product(1004, "h456wer53", "Bracelet", "Product Description", "bracelet.jpg", 15, "Accessories", 73, InventoryStatus.INSTOCK, 4));
products.add(new Product(1005, "av2231fwg", "Brown Purse", "Product Description", "brown-purse.jpg", 120, "Accessories", 0, InventoryStatus.OUTOFSTOCK, 4));
products.add(new Product(1006, "bib36pfvm", "Chakra Bracelet", "Product Description", "chakra-bracelet.jpg", 32, "Accessories", 5, InventoryStatus.LOWSTOCK, 3));
products.add(new Product(1007, "mbvjkgip5", "Galaxy Earrings", "Product Description", "galaxy-earrings.jpg", 34, "Accessories", 23, InventoryStatus.INSTOCK, 5));
products.add(new Product(1008, "vbb124btr", "Game Controller", "Product Description", "game-controller.jpg", 99, "Electronics", 2, InventoryStatus.LOWSTOCK, 4));
products.add(new Product(1009, "cm230f032", "Gaming Set", "Product Description", "gaming-set.jpg", 299, "Electronics", 63, InventoryStatus.INSTOCK, 3));
products.add(new Product(1010, "plb34234v", "Gold Phone Case", "Product Description", "gold-phone-case.jpg", 24, "Accessories", 0, InventoryStatus.OUTOFSTOCK, 4));
products.add(new Product(1011, "4920nnc2d", "Green Earbuds", "Product Description", "green-earbuds.jpg", 89, "Electronics", 23, InventoryStatus.INSTOCK, 4));
products.add(new Product(1012, "250vm23cc", "Green T-Shirt", "Product Description", "green-t-shirt.jpg", 49, "Clothing", 74, InventoryStatus.INSTOCK, 5));
products.add(new Product(1013, "fldsmn31b", "Grey T-Shirt", "Product Description", "grey-t-shirt.jpg", 48, "Clothing", 0, InventoryStatus.OUTOFSTOCK, 3));
products.add(new Product(1014, "waas1x2as", "Headphones", "Product Description", "headphones.jpg", 175, "Electronics", 8, InventoryStatus.LOWSTOCK, 5));
products.add(new Product(1015, "vb34btbg5", "Light Green T-Shirt", "Product Description", "light-green-t-shirt.jpg", 49, "Clothing", 34, InventoryStatus.INSTOCK, 4));
products.add(new Product(1016, "k8l6j58jl", "Lime Band", "Product Description", "lime-band.jpg", 79, "Fitness", 12, InventoryStatus.INSTOCK, 3));
products.add(new Product(1017, "v435nn85n", "Mini Speakers", "Product Description", "mini-speakers.jpg", 85, "Clothing", 42, InventoryStatus.INSTOCK, 4));
products.add(new Product(1018, "09zx9c0zc", "Painted Phone Case", "Product Description", "painted-phone-case.jpg", 56, "Accessories", 41, InventoryStatus.INSTOCK, 5));
products.add(new Product(1019, "mnb5mb2m5", "Pink Band", "Product Description", "pink-band.jpg", 79, "Fitness", 63, InventoryStatus.INSTOCK, 4));
products.add(new Product(1020, "r23fwf2w3", "Pink Purse", "Product Description", "pink-purse.jpg", 110, "Accessories", 0, InventoryStatus.OUTOFSTOCK, 4));
products.add(new Product(1021, "pxpzczo23", "Purple Band", "Product Description", "purple-band.jpg", 79, "Fitness", 6, InventoryStatus.LOWSTOCK, 3));
products.add(new Product(1022, "2c42cb5cb", "Purple Gemstone Necklace", "Product Description", "purple-gemstone-necklace.jpg", 45, "Accessories", 62, InventoryStatus.INSTOCK, 4));
products.add(new Product(1023, "5k43kkk23", "Purple T-Shirt", "Product Description", "purple-t-shirt.jpg", 49, "Clothing", 2, InventoryStatus.LOWSTOCK, 5));
products.add(new Product(1024, "lm2tny2k4", "Shoes", "Product Description", "shoes.jpg", 64, "Clothing", 0, InventoryStatus.INSTOCK, 4));
products.add(new Product(1025, "nbm5mv45n", "Sneakers", "Product Description", "sneakers.jpg", 78, "Clothing", 52, InventoryStatus.INSTOCK, 4));
products.add(new Product(1026, "zx23zc42c", "Teal T-Shirt", "Product Description", "teal-t-shirt.jpg", 49, "Clothing", 3, InventoryStatus.LOWSTOCK, 3));
products.add(new Product(1027, "acvx872gc", "Yellow Earbuds", "Product Description", "yellow-earbuds.jpg", 89, "Electronics", 35, InventoryStatus.INSTOCK, 3));
products.add(new Product(1028, "tx125ck42", "Yoga Mat", "Product Description", "yoga-mat.jpg", 20, "Fitness", 15, InventoryStatus.INSTOCK, 5));
products.add(new Product(1029, "gwuby345v", "Yoga Set", "Product Description", "yoga-set.jpg", 20, "Fitness", 25, InventoryStatus.INSTOCK, 8));
}
public List<Product> getProducts() {
return new ArrayList<>(products);
}
public List<Product> getProducts(int size) {
if (size > products.size()) {
Random rand = new Random();
List<Product> randomList = new ArrayList<>();
for (int i = 0; i < size; i++) {
int randomIndex = rand.nextInt(products.size());
randomList.add(products.get(randomIndex));
}
return randomList;
}
else {
return new ArrayList<>(products.subList(0, size));
}
}
public List<Product> getClonedProducts(int size) {
List<Product> results = new ArrayList<>();
List<Product> originals = getProducts(size);
for (Product original : originals) {
results.add(original.clone());
}
return results;
}
}
@Named("deLazyView")
@ViewScoped
public class LazyView implements Serializable {
private LazyDataModel<Customer> lazyModel;
private List<Customer> filteredCustomers;
@Inject
private CustomerService service;
@PostConstruct
public void init() {
lazyModel = new LazyCustomerDataModel(service.getCustomers(200));
}
public LazyDataModel<Customer> getLazyModel() {
return lazyModel;
}
public List<Customer> getFilteredCustomers() {
return filteredCustomers;
}
public void setFilteredCustomers(List<Customer> filteredCustomers) {
this.filteredCustomers = filteredCustomers;
}
public void setService(CustomerService service) {
this.service = service;
}
}
@Named
@ApplicationScoped
public class CustomerService {
List<Customer> customers;
private Country[] countries;
private Representative[] representatives;
private String[] names;
private String[] companies;
@PostConstruct
public void init() {
customers = new ArrayList<>();
countries = new Country[]{
new Country(0, "Argentina", "ar"),
new Country(1, "Australia", "au"),
new Country(2, "Brazil", "br"),
new Country(3, "Canada", "ca"),
new Country(4, "Germany", "de"),
new Country(5, "France", "fr"),
new Country(6, "India", "in"),
new Country(7, "Italy", "it"),
new Country(8, "Japan", "jp"),
new Country(9, "Russia", "ru"),
new Country(10, "Spain", "es"),
new Country(11, "United Kingdom", "gb")};
companies = new String[]{"Benton, John B Jr", "Chanay, Jeffrey A Esq", "Chemel, James L Cpa", "Feltz Printing Service",
"Printing Dimensions", "Chapman, Ross E Esq", "Morlong Associates", "Commercial Press", "Truhlar And Truhlar Attys",
"King, Christopher A Esq","Dorl, James J Esq","Rangoni Of Florence","Feiner Bros","Buckley Miller Wright","Rousseaux, Michael Esq",};
representatives = new Representative[]{new Representative("Amy Elsner", "amyelsner.png"), new Representative("Anna Fali", "annafali.png"),
new Representative("Asiya Javayant", "asiyajavayant.png"), new Representative("Bernardo Dominic", "bernardodominic.png"),
new Representative("Elwin Sharvill", "elwinsharvill.png"), new Representative("Ioni Bowcher", "ionibowcher.png"),
new Representative("Ivan Magalhaes", "ivanmagalhaes.png"), new Representative("Onyama Limba", "onyamalimba.png"),
new Representative("Stephen Shaw", "stephenshaw.png"), new Representative("Xuxue Feng", "xuxuefeng.png")};
names = new String[]{"James Butt", "David Darakjy", "Jeanfrancois Venere", "Ivar Paprocki", "Tony Foller",
"Adams Morasca", "Claire Tollner", "Costa Dilliard", "Juan Wieser", "Maria Marrier", "Jennifer Amigon",
"Stacey Maclead", "Leja Caldarera", "Morrow Ruta", "Arvin Albares", "Darci Poquette", "Izzy Garufi",
"Ricardo Gaucho", "Clifford Rim", "Emily Whobrey", "Kadeem Flosi", "Mujtaba Nicka", "Aika Inouye",
"Mayumi Kolmetz", "Misaki Royster", "Silvio Slusarski", "Nicolas Iturbide", "Antonio Caudy", "" +
"Deepesh Chui", "Aditya Kusko", "Aruna Figeroa", "Jones Vocelka", "Julie Stenseth", "Smith Glick",
"Johnson Sergi", "Francesco Shinko", "Salvatore Stockham", "Kaitlin Ostrosky", "Faith Gillian",
"Maisha Rulapaugh", "Jefferson Schemmer", "Leon Oldroyd", "Rodrigues Campain", "Alejandro Perin",
"Munro Ferencz", "Cody Saylors", "Chavez Briddick", "Sinclair Waycott", "Isabel Bowley", "Octavia Malet",
"Murillo Malet", "Greenwood Bolognia", "Wickens Nestle", "Ashley Doe"};
}
public List<Customer> getCustomers(int number) {
List<Customer> customers = new ArrayList<>();
for (int i = 0; i < number; i++) {
customers.add(new Customer(i + 1000, getName(), getCompany(), getCountry(), getDate(), CustomerStatus.random(), getActivity(), getRepresentative()));
}
return customers;
}
public List<Country> getCountries() {
return Arrays.asList(countries);
}
public CustomerStatus[] getCustomerStatus() {
return CustomerStatus.values();
}
public List<Representative> getRepresentatives() {
return Arrays.asList(representatives);
}
private String getName() {
return names[(int) (Math.random() * names.length)];
}
private Country getCountry() {
return countries[(int) (Math.random() * countries.length)];
}
private String getCompany() {
return companies[(int) (Math.random() * companies.length)];
}
private LocalDate getDate() {
LocalDate now = LocalDate.now();
long randomDay = ThreadLocalRandom.current().nextLong(now.minusDays(30).toEpochDay(), now.toEpochDay());
return LocalDate.ofEpochDay(randomDay);
}
private int getActivity() {
return (int) (Math.random() * 100);
}
private Representative getRepresentative() {
return representatives[(int) (Math.random() * representatives.length)];
}
}
public class TextExporter extends DataTableExporter {
private OutputStreamWriter osw;
private PrintWriter writer;
@Override
protected void preExport(FacesContext context, ExportConfiguration exportConfiguration) throws IOException {
osw = new OutputStreamWriter(getOutputStream(), exportConfiguration.getEncodingType());
writer = new PrintWriter(osw);
if (exportConfiguration.getPreProcessor() != null) {
exportConfiguration.getPreProcessor().invoke(context.getELContext(), new Object[]{writer});
}
}
@Override
protected void doExport(FacesContext context, DataTable table, ExportConfiguration exportConfiguration, int index) throws IOException {
writer.append("" + table.getId() + "\n");
if (exportConfiguration.isPageOnly()) {
exportPageOnly(context, table, writer);
} else if (exportConfiguration.isSelectionOnly()) {
exportSelectionOnly(context, table, writer);
} else {
exportAll(context, table, writer);
}
writer.append("" + table.getId() + "");
table.setRowIndex(-1);
}
@Override
protected void postExport(FacesContext context, ExportConfiguration exportConfiguration) throws IOException {
if (exportConfiguration.getPostProcessor() != null) {
exportConfiguration.getPostProcessor().invoke(context.getELContext(), new Object[]{writer});
}
writer.flush();
writer.close();
writer = null;
osw.close();
osw = null;
}
@Override
protected void preRowExport(DataTable table, Object document) {
((PrintWriter) document).append("\t" + table.getVar() + "\n");
}
@Override
protected void postRowExport(DataTable table, Object document) {
((PrintWriter) document).append("\t" + table.getVar() + "\n");
}
@Override
protected void exportCells(DataTable table, Object document) {
PrintWriter writer = (PrintWriter) document;
for (UIColumn col : table.getColumns()) {
if (col instanceof DynamicColumn) {
((DynamicColumn) col).applyStatelessModel();
}
if (col.isRendered() && col.isExportable()) {
String columnTag = getColumnTag(col);
addColumnValue(writer, col.getChildren(), columnTag, col);
}
}
}
protected String getColumnTag(UIColumn column) {
String headerText = (column.getExportHeaderValue() != null) ? column.getExportHeaderValue() : column.getHeaderText();
UIComponent facet = column.getFacet("header");
String columnTag;
if (headerText != null) {
columnTag = headerText.toLowerCase();
}
else if (facet != null) {
columnTag = exportValue(FacesContext.getCurrentInstance(), facet).toLowerCase();
}
else {
throw new FacesException("No suitable xml tag found for " + column);
}
return EscapeUtils.forXmlTag(columnTag);
}
protected void addColumnValue(PrintWriter writer, List<UIComponent> components, String tag, UIColumn column) {
FacesContext context = FacesContext.getCurrentInstance();
writer.append("\t\t" + tag + "");
if (column.getExportFunction() != null) {
writer.append(EscapeUtils.forXml(exportColumnByFunction(context, column)));
}
else {
for (UIComponent component : components) {
if (component.isRendered()) {
String value = exportValue(context, component);
if (value != null) {
writer.append(EscapeUtils.forXml(value));
}
}
}
}
writer.append("" + tag + "\n");
}
@Override
public String getContentType() {
return "text/plain";
}
@Override
public String getFileExtension() {
return ".txt";
}
}