how to remove table header in the csv file export

how to remove table header in the csv file export

KEshav09KEshav09 Posts: 18Questions: 9Answers: 0

HI team , i have made modifications to the table headers in my datatable like an addition on checkboxes and filtering through these check boxes . But the problem i see is when i download my csv file i see these custom header components in the downloaded csv file as well
Decentro URN |Bank Ref| Date/time| statusSUCCESSSort xClear Filter| providerHDFCYES BANKSort xClear Filter| txn typeIMPSUPISort xClear Filter| amount |Customer ref num
so this is the headers i get when i download them and even when i add custom headers i still this line coming there in the file export
Can someone help me with this

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    use export options. Here is an example:

    exportOptions: {
        format: {
            header: function ( data, column ) {
                //replace html tags with one space
                return data.replace(/<[^>]*>/g, ' ');
            }
        }
    }
    

    and the docs show you where to insert the code above (with your modifications of course):
    https://datatables.net/reference/button/csvHtml5

  • KEshav09KEshav09 Posts: 18Questions: 9Answers: 0

    Hi @rf1234 Thanks for this its working but still i am seeing
    Decentro URN Bank Ref Date/time status SUCCESS Sort x Clear Filter provider HDFC YES BANK Sort x Clear Filter txn type IMPS UPI Sort x Clear Filter amount
    this the tags are removed thanks to you yet the text under the tags are not removed

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It's likely it isn't supported, please see this thread. If you feel that doesn't apply, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited May 2022

    It was just an example ... not more. It only replaces the HTML tags ...

    You can return space from the function then you might have an empty row - or maybe even the result you want. Just check it out. As far as I know you can't delete the entire row using export options you would probably need to use "customize" for that which is much more complicated to use. You can search the forum and the docs on how to use "customize".

    So maybe try this first:

    exportOptions: {
        format: {
            header: function ( data, column ) {
                return "";
            }
        }
    }
    

    or this

    exportOptions: {
        format: {
            header: function ( data, column ) {
                return null;
            }
        }
    }
    

    Just tested it myself: returning null causes an error. Returning "" causes an empty header row.

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    I just testet this:

    header: function ( data, column ) {
        //replace html tags with one space
    //                return data.replace(/<[^>]*>/g, ' ');
        if ( column == 0 ) {
            return "this is my awesome header text!"
        }
        return " ";
    }
    

    It gives you this:

    Returning one space for the other columns except for column zero helps to get the formatting applied that I use in "customize". If you return an empty space the formatting won't be applied. Interesting.

  • KEshav09KEshav09 Posts: 18Questions: 9Answers: 0

    @rf1234 Is there a way where we can target separate columns and give them different formatting options
    like - column 1 - should have this header name
    all column should have removed the html tags and made it empty
    format: {
    header: function(data,content, index ,column) {
    // Here 2 is the index of the column
    // whose header name we want to change(0 based)

                           index === 3 ? "Status" : content;
    
    
                           index === 4 ? "Provider" : content;
    
    
                           index === 5 ? "Transaction Type" : content;
    
    
                          return data.replace(/<[^>]*>/g, ' ');
    
    
    
    
    
                          //replace html tags with one space
    
    
    
                      }
    

    i have tried this but i am not able to target each and every column and give them separate format options under the exportOptions

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited May 2022

    You can do anything with "customize" - but it isn't easy!

    Just take a look at this:
    https://datatables.net/forums/discussion/comment/207839#Comment_207839

    And this:
    https://datatables.net/forums/discussion/72366/excel-export

    And search the forum, you'll find a lot more on this.

  • KEshav09KEshav09 Posts: 18Questions: 9Answers: 0

    i have solved The problem myself here is what i did , its quite simple
    header: function (data, content, index, column) {
    for(let i=0 ;i<=12;i++){
    if(content===i){
    console.log(this.header())
    return names[i]
    }
    }
    }
    Create a array of names that you want it to be the header names for the spreadsheet to be downloaded
    then use the content object from the header function which return the header index
    compare the content and a var and iterate to the number of columns that is in the table and create the array of names you want accordingly

  • KEshav09KEshav09 Posts: 18Questions: 9Answers: 0
    edited May 2022

    Please Close this Discussion , Take my last comment as the accepted answer Please

  • sRank1sRank1 Posts: 1Questions: 0Answers: 1
    Answer ✓

    you can add button through this instead and set header to false:

    $('#myTable').DataTable( {
        buttons: [
            {
                header: false,
                extend: 'csv',
                text: 'Copy all data',
                exportOptions: {
                    modifier: {
                        search: 'none'
                    }
                }
            }
        ]
    } );
    

    make sure to add buttons reference as well:
    - https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js
    - https://cdn.datatables.net/buttons/1.6.5/js/buttons.flash.min.js

    see reference here: https://datatables.net/reference/button/csv

Sign In or Register to comment.