首页 > 解决方案 > 将条件添加到 p:dataExporter 的目标属性中,以便一次导出两个不同选项卡中的两个数据表,以便在 primefaces 中表现出色

问题描述

我有两个单选按钮,并在我的表单中使用了 p:tabView 和两个选项卡。单击第一个单选按钮时,第一个选项卡启用,单击第二个单选按钮时,第二个选项卡启用。在两个选项卡中都有一个数据表。我想一次将数据表导出为两个选项卡的 excel 格式。为此,我使用 p:dataExporter。我很困惑是否可以在目标中添加条件语句,p:dataExporter因为对于第一个单选按钮,我必须提供第一个选项卡表的 id,而对于第二个单选按钮,我必须提供第二个选项卡表的 id。如何才能做到这一点 ?我的代码是这样的:

<h:form id="form" target="_blank">
<p:blockUI block="form" trigger="cmdView">
    <p:graphicImage value="/resources/img/ajax-load.gif" />
</p:blockUI>
<p:panel header="#{text.form}">

    <h:panelGrid columns="5">
        <h:outputText for="console" value="SearchType" />

        <p:selectBooleanCheckbox id="isBrief" value="#{customerMB.brief}">
            <p:ajax listener="#{customerMB.unCheckDetail}"
                update="isDetail tabview"></p:ajax>
        </p:selectBooleanCheckbox>

        <h:outputText value="#{text.Brief} " />
        <p:selectBooleanCheckbox id="isDetail" value="#{customerMB.detail}">
            <p:ajax listener="#{customerMB.unCheckBrief}"
                update="isBrief tabview"></p:ajax>
        </p:selectBooleanCheckbox>
        <h:outputText value="#{text.Detail}" />
    </h:panelGrid>


    <p:commandButton id="cmdView" icon="fa fa-search-plus"
        value="#{text.View}" actionListener="#{customerMB.generateList}"
        process="@this" update="@form" />


    <p:commandButton id="cmdExcel" value="#{text.Excel}"
        disabled="#{empty customerMB.formList}" icon="fa fa-file-excel-o"
        ajax="false">
        <p:dataExporter type="xls" target="form:tabview:table1"
            fileName="form" postProcessor="#{customerMB.postProcessXLS}" />
    </p:commandButton>
</p:panel>
<p:tabView id="tabview" activeIndex="#{customerMB.activeTab}">
    <p:tab title="#{text.CustReportBrief}" id="tab1"
        disabled="#{customerMB.detail}">

        <p:dataTable id="table1" var="tblA" scrollable="true"
            resizableColumns="true" value="#{customerMB.List1}"
            rowIndexVar="rowSn" paginator="true">
            <p:column headerText="#{text.SNo}" width="50">
                <h:outputText value="#{tblA.sn}" />
            </p:column>

            <p:column headerText="#{text.MemberName}">
                <h:outputText value="#{tblA.customerName}" />
            </p:column>
            <p:column headerText="#{text.Address}">
                <h:outputText value="#{tblA.custAddress}" />
            </p:column>

        </p:dataTable>

    </p:tab>
    <p:tab title="#{text.CustReportDetail}" id="tab2"
        disabled="#{customerMB.brief}">

        <p:dataTable var="tblB" rowIndexVar="rowSn" scrollable="true"
            resizableColumns="true" value="#{customerMB.List2}" id="table2">
            <p:column headerText="#{text.SNo}" width="50px;">
                <h:outputText value="#{tblB.sn}" />
            </p:column>

            <p:column headerText="#{text.Name}">
                <h:outputText value="#{tblB.customerName}" />
            </p:column>
            <p:column headerText="#{text.Address}">
                <h:outputText value="#{tblB.custAddress}" />
            </p:column>
            <p:column headerText="#{text.Contact}">
                <h:outputText value="#{tblB.custContact}" />
            </p:column>
            <p:column headerText="#{text.Email}">
                <h:outputText value="#{tblB.custEmail}" />
            </p:column>



        </p:dataTable>

    </p:tab>
</p:tabView>
</h:form>

标签: excelprimefacesapache-poi

解决方案


我以前遇到过这个问题,我用两个未显示的数据解决了这个问题,commandButton里面有 dataExporter 并用commandButton.

    <p:commandButton id="cmdExcel" value="#{text.Excel}"
        disabled="#{empty customerMB.formList}" icon="fa fa-file-excel-o"
        onstart="document.getElementById('btn1').click()" 
        oncomplete="document.getElementById('btn2').click()"/>
    <p:commandButton id="btn1" ajax="false" style="display: none;">
        <p:dataExporter target="table1" type="xls" fileName="table1"/>
    </p:commandButton>
    <p:commandButton id="btn2" ajax="false" style="display: none;">
        <p:dataExporter target="table2" type="xls" fileName="table2"/>
    </p:commandButton>

推荐阅读