What am I doing wrong with my export settings?

What am I doing wrong with my export settings?

lordterrinlordterrin Posts: 22Questions: 10Answers: 1

I know you like to see a live, reproducible database to troubleshoot @Allan, but I cannot provide one in this instance. I'm playing around with the customize exporting function, but I'm finding that the including of it breaks my table. If I include the following code, the "Export to Excel" button only appears about 25% of the time - most of the time it just does not show up, and it's not in the the raw HTML either.

What's happening here is that my user imports a spreadsheet originally, I do a bunch of stuff to it and create new columns on their spreadsheet, then give it back to them. I'm trying to highlight those new columns so they know what they are. Like I said, this code DOES work perfectly, but only about 25% of the time, and I cannot see why....

customize : function ( xlsx ){
                                      var sheet = xlsx.xl.worksheets['sheet1.xml'];        
               
                                      // https://datatables.net/reference/button/excelHtml5
                                      // Loop through each line in th header row, if the value of the cell is something in the headers var, it came from the original file, otherwise, it's something
                                      // we added, so change the background color of the cell in Excel
                                      $('row c[r*="1"]', sheet).each( function () {
                                          var this_header = $('is t', this).text();
                                          if ( $.inArray(this_header, export_columns) !== -1 ) {
                                            $(this).attr( 's', '35' );  
                                          }
                                      });                                     
                        },

If I take this piece of code OUT however, it works just fine. At the time of generation, the export_columns array exists, so the jQuery should be executing just fine. I personally do not see any problems from a straight coding perspective - I'm just wondering if anything catches your eye as to why the code works SOMETIMES, provides me with an Excel button to click, and generates the table EXACTLY like I expect it to, but most of the time does not generate a button for me to click and does not include the generated HTML for the button.

 'buttons'         : [
                        { extend    : 'excelHtml5', 
                          text      : '<img class="format_3_excel_export_button" src="../../img/excel_small.png" title="Export this entire table to Excel"><div class="bom_download_text">Download your file</div>', 
                         customize : function ( xlsx ){
                                      var sheet = xlsx.xl.worksheets['sheet1.xml'];        
               
                                      // https://datatables.net/reference/button/excelHtml5
                                      // Loop through each line in th header row, if the value of the cell is something in the headers var, it came from the original file, otherwise, it's something
                                      // we added, so change the background color of the cell in Excel
                                      $('row c[r*="1"]', sheet).each( function () {
                                          var this_header = $('is t', this).text();
                                          if ( $.inArray(this_header, export_columns) !== -1 ) {
                                            $(this).attr( 's', '35' );  
                                          }
                                      });                                     
                        },        
                          title     : 'Export - ' + export_date 
                        }
                      ],

Here is the entire function that runs to generate the table: I show 0 rows because in this case the sole purpose of datatables is to export the modified file back to the user - not to display anything on screen. Thus, if the download button isn't showing up, nothing works. :) I can completely take out the customize function and the code works 100% of the time, (so that's what I'm doing in production...) I'd just love to make it work if I could.......

function bom_table(dt,columns_list, export_columns) {

if ( $.fn.dataTable.isDataTable( '#bom_table' ) ) {      
    bom_table.draw();
    bom_table.destroy();          
    }

$.fn.dataTable.ext.errMode = 'throw';            
bom_table = $('#bom_table').DataTable( {    
  data              : dt,                               
  'bInfo'           : false,     // turns off the header
  'paging'          : true,     // disables paging, so all results are loaded in one table.         8928 / HO
  'pageLength'      : 0,
  'bAutoWidth'      : false,
  'bFilter'         : false,      // disables search 
  'dom'               : 'Bfrti',
  'buttons'         : [
                        { extend    : 'excelHtml5', 
                          text      : '<img class="format_3_excel_export_button" src="../../img/excel_small.png" title="Export this entire table to Excel"><div class="bom_download_text">Download your file</div>', 
                         customize : function ( xlsx ){
                                      var sheet = xlsx.xl.worksheets['sheet1.xml'];                       
                                      // https://datatables.net/reference/button/excelHtml5
                                      // Loop through each line in th header row, if the value of the cell is something in the headers var, it came from the original file, otherwise, it's something
                                      // we added, so change the background color of the cell in Excel
                                      $('row c[r*="1"]', sheet).each( function () {
                                          var this_header = $('is t', this).text();
                                          if ( $.inArray(this_header, export_columns) !== -1 ) {
                                            $(this).attr( 's', '35' );  
                                          }
                                      });                                     
                        },        
                          title     : 'Export - ' + export_date 
                        }
                      ],                       
  'columns'         : columns_list,
  'language'        : { 
                      'loadingRecords' : '<div class="loadingCenter">Loading..........<i class="fa fa-circle-o-notch fa-spin"></i></div>',
                      'emptyTable'     : '<div class="noDataCenter">Sorry, no records were found to display.  Either you have search settings <span id="hoverButton" class="fa-stack"><i class="fa fa-square fa-stack-2x"></i><i class="fa fa-bars fa-stack-1x fa-white"></i></span> that are too restrictive, or you have searched for too many parts at once.</div>',      
                      'zeroRecords'    : '<div class="noDataCenter">There were no records in your uploaded file..</div>'
                      }                                     
});

}

This question has an accepted answers - jump to answer

Answers

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28

    Your customize code works OK I think. See a little codepen test HERE
    Is the variable export_date always set? I ask this because when I remove the line for this var in my example, the button disappears.

  • lordterrinlordterrin Posts: 22Questions: 10Answers: 1

    Yes - export date is always defined. I can comment out JUST the customize code and the export button appears 100% of the time. It's only when I include it that I have about a 25% hit rate... If something works 0% of the time, it's easier to debug.... :smiley:

  • kthorngrenkthorngren Posts: 20,140Questions: 26Answers: 4,735
    Answer ✓

    I may have missed it but do you get console errors when the button doesn't appear?

    From my understanding the customize function doesn't run until you click the export button. Not sure how it would affect loading the button unless there is a syntax issue.

    Does the problem occur on certain types of browsers?

    Kevin

  • lordterrinlordterrin Posts: 22Questions: 10Answers: 1

    nope - no console errors - it appears as though it runs without a hitch. I can manually re-run the function, as I put the variables it needs in the function call, but in instances where it DOESN'T work, re-calling the function does nothing. I get the table, it's visible, but there's no export button. So odd!

This discussion has been closed.