![](/img/trans.png)
[英]Primefaces <p:dataExporter> does not produce Excel download
[英]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和兩個選項卡。 單擊第一個單選按鈕時,啟用第一個選項卡,單擊第二個單選按鈕時,啟用第二個選項卡。 在兩個選項卡中都有一個數據表。 我想一次將數據表導出為兩個選項卡的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>
我之前遇到過這個問題,我使用了兩個未顯示的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>
好的,我按照自己的方式做,這也很簡單。 我定義了字符串變量以將表ID保存在custmerMB中,並根據選擇的單選按鈕在該變量中設置值。 代碼是這樣的:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.