Multiple exports of selected rows?

Multiple exports of selected rows?

j e harmsj e harms Posts: 7Questions: 3Answers: 0
edited March 2017 in Select

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

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    Without being able to see the code you have written, its very difficult to say exactly what is going on I'm afraid.

    Allan

  • j e harmsj e harms Posts: 7Questions: 3Answers: 0

    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.

    { // custom button for billing to firms
      text: 'Bill to Firms',
      className: 'bill',
      action: function (e, dt){
      firmCounter =1;
      var firmNo =  dt.cell(0,12).data();
      dt.column(12,{page:'current'} ).data().each( function ( firm_Id, i ) {
           dt.rows(i).select();     //select each row until the firm_Id changes
           if(firm_Id !== firmNo){
               firmCounter++;
               dt.rows(i).deselect();  //deselect this row (with new firm_Id) to allow exporting of selected rows 
               firmNo = firm_Id;
               table.buttons('.pdfBill').trigger(); // export selected rows to PDF
               dt.rows().deselect(); 
               dt.rows(i).select(); // reselect the current row (1st row for the next firm) 
           }
        });
    
    

    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.

  • j e harmsj e harms Posts: 7Questions: 3Answers: 0

    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:

    exportOptions:{
      rows: rowArr,
    }
    

    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:

    [0,1,2,3,4,5,6,7,8,9]
    

    Just before it is triggered the second time, the array looks like this:

    [10,11]
    

    The result is opposite to the result from exporting selected rows like this:

    exportOptions:{
       rows: {selected: true},
    }
    

    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.

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    Answer ✓

    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:

    1. Change pdfmake to have a sync option
    2. Add a 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

  • j e harmsj e harms Posts: 7Questions: 3Answers: 0

    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.

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    Don't blame you - its massive!

    Look forward to hearing what you come up with.

    Allan

This discussion has been closed.