Export to excel error when having dynamic table columns

Export to excel error when having dynamic table columns

nadeemahmadnadeemahmad Posts: 4Questions: 0Answers: 0
edited July 2018 in Free community support

Thanks in advance!!!

var tableToExport;
     $.ajax( {
         "url": contextPath + '/getData',
         "type" : "POST",
         "success": function ( json ) {         
              tableToExport=  $('#reportList').dataTable( {
                 "data":json["data"],
                 "columns":json["columns"],
                 dom: 'B',
                 buttons: [
                            {
                                extend: 'excelHtml5',
                                title: 'Report',
                                text:      '<i class="fa fa-file-excel-o"></i>',
                                titleAttr: 'Excel',
                                'exportOptions':
                                {                                
                                },
                                customize: function ( xslx ) {                                  
                                }
                            }
                        ],
                  "drawCallback": function( settings ) {
                      tableToExport.button().trigger();
                       }
             } );
         },
         "dataType": "json"
     } );

I don't know but this isn't working, getting "Uncaught TypeError: Cannot read property 'button' of undefined" error when it comes to "drawCallback".

Can anyone help?

Replies

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @nadeemahmad ,

    The problem is on line 24, tableToExport.button().trigger(). tableToExport won't be visible at that point in the drawCallback (see the second example on that page), so try this.api().button().trigger() instead,

    Cheers,

    Colin

  • nadeemahmadnadeemahmad Posts: 4Questions: 0Answers: 0

    Thank you @colin
    That was really helpful.

This discussion has been closed.