Nick
Nick

Reputation: 91

Primefaces ExcelExporter with CommandLinks

I have an application that contains commandLinks within a Primefaces dataTable. The commandLinks link to other pages within the application and pass parameters. When I try to export the dataTable using Primefaces' ExcelExporter, the generated .xls file contains the value attribute for the commandLink, not the value attribute for the outputText nested within the commandLink.

Column within dataTable code:

<p:dataTable var="dataRow" id="myTable">
    <p:column>
        <f:facet name="header">
            <h:outputText value="MyColumn" />
        </f:facet>
        <h:outputLink value="myPage.xhtml">
            <f:param name="columnId" value="#{dataRow.columnId}" />
            <h:outputText value="#{dataRow.columnName }" />
       </h:outputLink>
    </p:column>
</p:dataTable>

ExcelExporter code:

<h:commandLink>
    <h:outputText value="Export" />
       <p:dataExporter type="xls" target="myTable" fileName="tableResults"/>
</h:commandLink>

When I export the table using the ExcelExporter, the exported data is "myPage.xhtml", when I want it to be the data contained in "#{dataRow.columnId}". Is there a way to format the links so they are exported with the text that I want?

Upvotes: 3

Views: 1529

Answers (1)

Nick
Nick

Reputation: 91

I was able to solve this problem by changing the links to commandLinks with actions that determine navigation and setPropertyActionListeners to pass parameters. It looks like PrimeFaces always takes the value from the parent component, so this seemed to be the best workaround. The dataExporter code stayed the same.

Modified xhtml code:

<p:dataTable var="dataRow" id="myTable">
    <p:column>
        <f:facet name="header">
            <h:outputText value="MyColumn" />
        </f:facet>
        <h:commandLink value="#{dataRow.columnName}" action="myPage">
            <f:setPropertyActionListener target="#{myPage.columnId}" 
               value="#{dataRow.columnId}"/> 
       </h:commandLink>
    </p:column>
</p:dataTable>

Navigation rule added to faces-config.xml:

<navigation-rule>
  <navigation-case>
       <from-outcome>myPage</from-outcome>
       <to-view-id>/myPage.xhtml</to-view-id>
       <redirect />
   </navigation-case>
</navigation-rule>

Upvotes: 1

Related Questions