jsfprimefacesprimefaces-extensions

Non rendered p:dataTable ends up in pe:exporter PDF


I have 2 dataTables:

<p:dataTable id="tbl1" var="prop1" value="#{bean.prop1}" rendered="#{bean.listP1.size() != 0}">
// ...
</p:dataTable>

and

<p:dataTable id="tbl2" var="prop2" value="#{bean.prop2}" rendered="#{bean.listP2.size() != 0}">
// ...
</p:dataTable>

On XHTML page, after some action, I got the correct result depending on the size of both listP1 and listP2.

My problem is after clicking on export button

<h:commandLink>
    <p:graphicImage value="/resources/icons/download.png" style="width : 35px; height:35px"/>
    <pe:exporter type="pdf" target="tbl1, tbl2" fileName="SurveyResults"/>
</h:commandLink>

I got wrong result : I got the two tables instead of one because one of them is with size = 0

Have you please any idea about solving that?


Solution

  • Prior to version 8, the pe:exporter does not care whether the data tables you provide are rendered or not, see https://github.com/primefaces-extensions/primefaces-extensions.github.com/issues/757

    If you are not on version 8 yet, you can use EL to create a dynamic value for the target attribute though. For example:

    <pe:exporter type="pdf"
                 target="#{empty bean.listP1 ? '' : 'tbl1'}#{empty bean.listP1 or empty bean.listP2 ? '' : ','}#{empty bean.listP2 ? '' : 'tbl2'}"
                 fileName="SurveyResults"/>
    

    As these expressions are a bit hairy, you might want to create a method in your bean which creates a target string and do:

    <pe:exporter type="pdf"
                 target="#{bean.exporterTargets}"
                 fileName="SurveyResults"/>
    

    And in your bean for example:

    public String getExporterTargets() {
       return Stream.of(listP1.isEmpty() ? null : "tbl1",
                        listP2.isEmpty() ? null : "tbl2")
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining(","));
    }