filter and export excel

filter and export excel

montoyammontoyam Posts: 568Questions: 136Answers: 5
edited August 2020 in Buttons

I have a button where I would like to first apply a search to a table and then export it to excel. Is that possible? Right now I have them as two separate buttons just to test.

                {
                    text: 'filter', 
                    action: function (e, dt, node, config) {
                        InvoiceListingTable.search('7/1/2020');
                        InvoiceListingTable.draw();
                    }
                },
                {
                    extend: 'excelHtml5',
                    text: 'Auditor spreadsheet',
                    exportOptions: {
                        columns: '.excelExport'
                    },
                    customize: function (xlsx) {
                        var sheet = xlsx.xl.worksheets['sheet1.xml'];
                        $('c[r=A1] t', sheet).text('ITC/Telecom Billing ' + AsOfCookie);
                        //get column A and assign '50' to the s tag (50=left align)
                        $('row c[r^="A"]', sheet).attr('s', '50');   //https://datatables.net/reference/button/excelHtml5
                    }
                }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Yes - see the buttons.buttons.action page, specifically the last example on that page, to be able to call the built in button actions from a custom function.

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited August 2020

    Nice. But, can I add my customization to that?

                        customize: function (xlsx) {
                            var sheet = xlsx.xl.worksheets['sheet1.xml'];
                            $('c[r=A1] t', sheet).text('ITC/Telecom Billing ' + AsOfCookie);
                            //get column A and assign '50' to the s tag (50=left align)
                            $('row c[r^="A"]', sheet).attr('s', '50');   //https://datatables.net/reference/button/excelHtml5
                            $('row c[r^="B"]', sheet).attr('s', '50');  
                            $('row c[r^="D"]', sheet).attr('s', '64');  
                            $('row c[r^="E"]', sheet).attr('s', '64');  
                        }
    

    and...

                        exportOptions: {
                            columns: '.excelExport'
                        },
    
    
  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,948

    Did you try adding the buttons.buttons.action to your button? Seems to work here:
    http://live.datatables.net/yogegosu/1/edit

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited August 2020

    oooh, I see. I thought I was getting rid of the extend: 'excelHtml5' and just having 'regular' button with an action that then ran the excel action.

    thank you for clarifying, yes, that code worked perfectly.

This discussion has been closed.