Christian O.
Christian O.

Reputation: 498

Exporting data from a dataTable whose ID has been set via a parameter

I have a composite component that uses a dataTable to display data and a dataExporter, like this:

The custom component:

<composite:interface>
    <composite:attribute name="viewModeName" type="java.lang.String" required="true"/>
    <composite:attribute name="data" type="org.primefaces.model.LazyDataModel" required="true"/>
    <composite:attribute name="widgetVarName" type="java.lang.String" required="false" default="defaultTableNameWidge"/>
    <composite:attribute name="id" type="java.lang.String" required="false" default="defaultTableNameID"/>

</composite:interface>

<composite:implementation>
    <p:outputPanel>
                <p:fieldset>
                    <p:dataTable id="#{cc.attrs.id}"
                                 emptyMessage="#{msgs.dataTable_emptyMessage}"
                                 var="myVar"
                                 value="#{cc.attrs.data}"
                                 lazy="true"
                                 rows="20"
                                 paginator="true"
                                 currentPageReportTemplate="({startRecord} - {endRecord} von {totalRecords})"
                                 paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                 widgetVar="#{cc.attrs.widgetVarName}">

             ... the structure of the table ...

                    </p:dataTable>
                    <div class="footer">
                        <div style="float: right;">

                            <p:commandButton value="Excel-Export" ajax="false" icon="fa fa-file-excel-o" title="someTitle" >
                                <p:dataExporter target="#{cc.attrs.id}" type="xls" postProcessor="#{documentService.postProcessXLS}"
                                    encoding="UTF-8"
                                    fileName="fn"></p:dataExporter>
                            </p:commandButton>

                        </div>
                    </div>
            </p:fieldset>
    </p:outputPanel>

As you can see, I set the id and widgetVar attributes via a parameter that is pass. This is because I use this component twice one the same page, and just using the same static value for id and widgetVar results in problems.

Here is the page that uses the component twice:

<ui:define name="content">
    <f:metadata>
            <f:viewParam name="viewMode" value="#{backendBean.viewMode}"/>
            <f:viewAction action="#{backendBean.init}"  />
    </f:metadata>
    <h:form id="frmTables" styleClass="frmTablesStyle">
    
                <customTableClass:customTable
                        viewModeName = "#{backendBean.viewMode.name()}"
                        data= "#{backendBean.dataAttribute}" />
    
                <c:if test="#{backendBean.viewMode.name() == 'FOREIGN'}">
    
                    <customTableClass:customTable
                            viewModeName = "FOREIGN"
                            data= "#{backendBean.otherDataAttribute}"
                            widgetVarName = "widgetPassedByParameter"
                            id = "idPassedByParameter" />
                </c:if>
    
     </h:form>
</ui:define>

This works mostly fine. However, since I changed the id and widgetVar values from static values to setting them via attributes, the dataExporter stopped working.

So the dataExporter works fine when I have my custom component like this:

...
<p:dataTable id="defaultTableNameID" ... />
...
<p:dataExporter target="defaultTableNameID" .../>

but not when I have it like this:

...
<composite:attribute name="id" type="java.lang.String" required="false" default="defaultTableNameID"/>
...
<p:dataTable id="#{cc.attrs.id}" ... />
...
<p:dataExporter target="#{cc.attrs.id}" .../>

This is the error I get:

Sep 02, 2021 10:57:21 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: Unsupported datasource target:"javax.faces.component.UINamingContainer", exporter must target a PrimeFaces DataTable.
javax.faces.FacesException: Unsupported datasource target:"javax.faces.component.UINamingContainer", exporter must target a PrimeFaces DataTable.

How do I properly reference the id that I set via parameter from the target attribute of the exporter?

Upvotes: 1

Views: 165

Answers (0)

Related Questions