繁体   English   中英

如何通过DataTable ID获取Primefaces dataTable的数据用于Javascript function?

[英]How to get the data of a Primefaces dataTable to use in a Javascript function by using the DataTable ID?

我有以下 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,因为用于导出到 excel 的名为 DataExporter 的 Primefaces 工具不能很好地处理 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 方式导出数据时,无论我尝试了什么,当数据列表数据作为 Javascript function 的输入时,数据列表数据为 null。这是我得到的错误: Uncaught TypeError: tableSelect is null 如何传递数据表中显示的值,以便将它们导出到 excel 文件?

与其尝试几乎不可能的黑客攻击,不如使用<p:dataExporter postProcessor="..."/>对导出的内容进行后处理。

看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM