如何使用 DataTable ID 获取 Primefaces dataTable 的数据以在 Javascript 函数中使用?

发布于 2025-01-11 08:09:11 字数 3058 浏览 1 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文