如何使用 DataTable ID 获取 Primefaces dataTable 的数据以在 Javascript 函数中使用?
我有以下 Primefaces 数据表(部分如下所示),它从数据库获取其值。
<p:dataTable id="datalist" value="#{debtPaymentsController.lazyDebtPayments}" var="item"
selectionMode="single" selection="#{debtPaymentsController.selected}"
lazy="true" widgetVar="debtPaymentsTable"
paginator="true" scrollable="true" scrollWidth="100%"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowKey="#{index}" rowIndexVar="index"
rows="10"
rowsPerPageTemplate="10,20,30,40,50">
</p:datatable>
我正在尝试使用 Javascript 脚本将数据表的内容导出到 Excel,因为称为 DataExporter 的 Primefaces 导出到 Excel 的工具不能很好地处理 url:
<script type="text/javascript">
function exportTableToExcel(tableID) {
console.log(tableID);
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
console.log(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = filename ? filename + '.xls' : 'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if (navigator.msSaveOrOpenBlob) {
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob(blob, filename);
} else {
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
</script>
这是我正在使用的按钮:
<p:commandButton id ="exportExcelFile" value="exportExcelFile" onclick="exportTableToExcel('datalist')"/>
当我使用 DataExporter 时,我正在运行以下内容似乎工作正常(除了 url 问题未在 Excel 上显示为超链接之外),因此我导出数据表上此特定时间显示的所有数据:
<p:dataExporter type="xls" target="datalist" fileName="payments"/>
不幸的是,当我使用 Javascript 时导出数据的方法,无论我尝试什么,当 datalist 数据作为 Javascript 函数的输入给出时,它都是 null。这是我收到的错误:Uncaught TypeError: tableSelect is null。如何传递数据表中显示的值,以便将它们导出到 Excel 文件?
I have the following Primefaces datatable(part of it is shown below)which gets its values from a database.
<p:dataTable id="datalist" value="#{debtPaymentsController.lazyDebtPayments}" var="item"
selectionMode="single" selection="#{debtPaymentsController.selected}"
lazy="true" widgetVar="debtPaymentsTable"
paginator="true" scrollable="true" scrollWidth="100%"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowKey="#{index}" rowIndexVar="index"
rows="10"
rowsPerPageTemplate="10,20,30,40,50">
</p:datatable>
I am trying to use a Javascript script to export the content of the datatable to excel, because the Primefaces tool to export to excel called DataExporter does not work well with urls:
<script type="text/javascript">
function exportTableToExcel(tableID) {
console.log(tableID);
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
console.log(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = filename ? filename + '.xls' : 'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if (navigator.msSaveOrOpenBlob) {
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob(blob, filename);
} else {
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
</script>
Here is the button I am using:
<p:commandButton id ="exportExcelFile" value="exportExcelFile" onclick="exportTableToExcel('datalist')"/>
When I use DataExporter I am running the following and it seems to work fine(other than the url problem not being shown as hyperlink on excel), so I export all the data that is being shown at this specific time on the datatable:
<p:dataExporter type="xls" target="datalist" fileName="payments"/>
Unfortunately,when I use the Javascript way to export the data, no matter what I have tried, datalist data is null when it is given as input to the Javascript function. This is the error I get: Uncaught TypeError: tableSelect is null. How can I pass the values that are being shown in the datatable so I can export them to an excel file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要尝试几乎不可能的破解,而是使用
对导出的内容进行后处理。请参阅:
Instead of trying a close to impossible hack, use
<p:dataExporter postProcessor="..."/>
to post process the contents of the export.See: