Multiple exports of selected rows?
Multiple exports of selected rows?
My data table has records for invoicing to multiple firms.
There is a "Bill to Firms" button that loops through all the rows in the table and does the following:
1. selects the rows for the first firm,
2. when all the rows for that firm have been selected, triggers the PDF export button to export the selected rows,
3. then deselects the rows for the first firm, selects the rows for the next firm and triggers the PDF export button for that firm,
4. and so on.
The issue: The PDF exports do not begin until the code has looped through all the rows. This means that when the PDF export for the first firm is executed, the selected rows are those that were selected for the last firm.
I inserted an 'alert' after triggering the PDF export in order to allow the PDF export to complete before moving on to the next firm, but the exports would not begin until the alert was closed.
This question has an accepted answers - jump to answer
Answers
Without being able to see the code you have written, its very difficult to say exactly what is going on I'm afraid.
Allan
The following code should illustrate my issue. It loops through all the rows, but stops when it reaches a new firm and triggers the button to export the selected rows. It then continues to select the rows for the next firm, and then triggers the export button again to export the newly selected rows.
My sample table has 12 rows: 10 for firm A and 2 for firm B. When I run this script, it generates two PDF files that are identical. They both show an export of the 2 rows for firm B.
I tried inserting an alert after the table.buttons.trigger command, but the export would not initiate until I dismissed the alert.
It appears that the triggered button does not activate until all the code with the custom button has finished executing.
I'd really appreciate any guidance you can provide.
I tried another method to export different sets of rows consecutively. I pushed the row numbers into an array ('rowArr') and used the array under export options like this:
When looping through the rows, the index of the row is pushed onto the array until the row is for the next firm. The PDF export button is then triggered and the array is emptied. The loop continues for the next firm.
Just before the PDF export button is triggered the first time, the array (as shown in the console log) looks like this:
Just before it is triggered the second time, the array looks like this:
The result is opposite to the result from exporting selected rows like this:
When I used the selected rows method, the two exported files were identical. They both contained the last 2 rows.
When I used the array method, the two exported files were still identical, but they each contained the 10 rows that belonged to the first firm.
Thanks for the code - that's very cunning!
What appears to be happening is that the pdfmake library that is being used by Buttons to create the files uses an async download action. In normal use, you'd never see the delay from the async action being queued to the file being downloaded since there is nothing else for the program to do.
However, in this case, as the browser is waiting for the main "thread" to complete running before carrying out any of the async actions, you get the delay / buffer effect that you are seeing.
This is the code in buttons that is being used to trigger the download. As you will be able to see it is using a callback for the
getBuffer
, which it has to do since this is async.There are two options I can think of:
setTimeout
into your code after each iteration of the loop (that will mean changing the structure of your code to use functions rather than a simple loop) allowing the browser to service the async actions.Allan
Thanks for your suggestion. I'll try your second suggestion first because I'd like to avoid delving into the pdfmake library.
If I come up with a solution (or more questions on this topic), I'll post accordingly.
Don't blame you - its massive!
Look forward to hearing what you come up with.
Allan