Can not format the table header (strip br tags) during excel export

Can not format the table header (strip br tags) during excel export

veronduijnaveronduijna Posts: 5Questions: 1Answers: 0

I have a datatable. There are <br> tags in table header. I want to strip during excel export. It is 2 days already can not find solution. Please help.
This example works for the body, but not for the header: https://datatables.net/extensions/buttons/examples/html5/outputFormat-function.html

=============== this is my code ==================

var exportFormatter = {
    format: {
        header: function (data, row, column, node) {
            return data.replace(/[<]br[^>]*[>]/gi," ");
        }
    }
};






table = $('#mydatatable').DataTable({

            order: [],

            buttons: [{
                    extend: 'excelHtml5',
                    filename: 'excelfilename', 
                    footer: false,
                    title: '',
                    text: '<i class="fa-solid fa-file-arrow-down"></i>&nbsp;&nbsp;<span>Excel</span>'                    
                }],

       initComplete: function () {

         /* some code for column filtering */

       }


});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,257Questions: 26Answers: 4,761

    There seems to be a problem with the exportFormatter example also. It seems Buttons 3.0.0. only processes the first row of data:

    Name    Position    Office  Extn.   Start date  Salary
    Airi Satou  Accountant  Tokyo   5407    2008-11-28  162700
    Angelica Ramos  Chief Executive Officer (CEO)   London  5797    2009-10-09  $1,200,000
    Ashton Cox  Junior Technical Author San Francisco   1562    2009-01-12  $86,000
    

    It may be the same issue you are having. @allan will need to take a look.

    Kevin

  • veronduijnaveronduijna Posts: 5Questions: 1Answers: 0

    Yes, It seems older versions works fine when it comes to exporting. But I have other features in my table which not included in older versions. So, I can not use them.
    This is my final code:

    buttons: [{
                        extend: 'excelHtml5',
                        filename: 'FileName', 
                        footer: false,
                        title: '',
                        text: '<i class="fa-solid fa-file-arrow-down"></i>&nbsp;&nbsp;<span>Excel</span>',
                        exportOptions: {
                          format: {
                            header: function (data, collindex, node) {
                              return data.replace(/[$,]/g, '');                         
                            },
                          },
                        }
                    }]
    
  • veronduijnaveronduijna Posts: 5Questions: 1Answers: 0

    when I try to log the data I get error about header[col].length in buttons.html5.min.js file. But it works fine when there is "body" instead of "header".

    format: {
                            header: function (data, collindex, node) {
                              console.log(data);
                            },
                          },
    
  • veronduijnaveronduijna Posts: 5Questions: 1Answers: 0

    anything?

  • allanallan Posts: 61,627Questions: 1Answers: 10,091 Site admin
    Answer ✓

    Have you tried updating to Buttons 3.0.1? It addresses a few issues with the export of the header and footer.

    If that doesn't address the issue, can you link to a test case showing the problem please.

    Allan

  • veronduijnaveronduijna Posts: 5Questions: 1Answers: 0

    Yes, it worked. Thanks.

Sign In or Register to comment.