将条件添加到 p:dataExporter 的目标属性中,以一次将两个不同选项卡中的两个数据表导出到 primefaces 中的 excel

Adding condition into target attribute for p:dataExporter to export two datatables in two different tabs one at a time to excel in primefaces

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

<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>

我以前遇到过这个问题,我用两个未显示的 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>

好的,我按照自己的方式做了,也很简单。我定义了字符串变量来保存 custmerMB 中的 table id,并根据所选的单选按钮设置该变量中的值。 代码是这样的:

private String tblToExport;

public String getTblToExport() {
    return tblToExport;
}

public void setTblToExport(String tblToExport) {
    this.tblToExport = tblToExport;
}

public void unCheckDetail() {

    if (isBrief == true) {
        tblToExport=":form:tabview:table1";

}

public void unCheckBrief() {

    if (isDetail == true) {
        tblToExport=":form:tabview:table2";

    } 
}

和excel按钮代码是:

<p:commandButton id="cmdExcel" value="#{text.Excel}"
    disabled="#{empty customerMB.formList}" icon="fa fa-file-excel-o"
    ajax="false">
    <p:dataExporter type="xls" target="#{customerMB.tblToExport}"
        fileName="form" postProcessor="#{customerMB.postProcessXLS}" />
</p:commandButton>