Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Instead of trying a close to impossible hack, use <p:dataExporter postProcessor="..."/> to post process the contents of the export.

See:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading