how to export selected column in excel using for each over the data table?

how to export selected column in excel using for each over the data table?

urharshurharsh Posts: 7Questions: 4Answers: 0

how to export selected column in excel using for each over the data table?

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    I would extend the excel button and used customizeData as shown in the example below.

    $('#example').DataTable({
                        /// blah blah blah
                    },
                    buttons: [{text:"Excel",
                        extend: 'excelHtml5',
                        customizeData: function (exData) {
                             // basically the data going to excel is in exData.body 
                            // right in this location so you can manipulate it as you see fit. 
                             // in the case below, I was hiding excel data where 
                            // where rows were hidden but not removed.
    
                            var rowNodes = aTable.rows().nodes();
                            var newData = []
                            for (var i = (exData.body.length - 1) ; i >= 0; i--) {
                             
                                if ($(rowNodes[i]).css("display") == "none") {
                                    continue;
                                }
                                newData[newData.length] = exData.body[i];
                            }
                            // the loop reverses order so put it back
                            exData.body = newData.reverse();
                           
                        }}]
                });
    
    
  • bindridbindrid Posts: 730Questions: 0Answers: 119

    However, if you are removing columns from the body, be sure to update the footer and header sections (also included in exData above).

  • urharshurharsh Posts: 7Questions: 4Answers: 0

    Thank you Bindrid. but i am unable to place debug point in inner part of
    => $('#example').DataTable({
    in chrome.
    pl. tell me if you have a way to do.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    I had no problem debugging with Chrome. If you go to http://jsbin.com/solipe/edit?html,js,output you will see I added the line debugger; inside the function so it looks like this:

      customizeData: function (exData) { 
        debugger;
    
    }, 
    
    

    I then started up Chrome and navigated to the above link.

    Once there, I clicked on the three vertical buttons, clicked on more tools then clicked on developer tools. That opened up the Chrome debugger.

    I then clicked on the Run with JS button which loaded up the DataTable. I then clicked on the DataTable Excel button. Code executed until it reached the "debugger;" line where it paused. That allowed me to examine all of the variables etc and to see what adjustments I had to make.

  • urharshurharsh Posts: 7Questions: 4Answers: 0

    vertical buttons:-> clicked on developer tools. >
    animation
    layers
    network conditions
    quicksource
    remotedevice
    rendering
    serach

    sensors

    but developer tools does't exists.

  • measterbromeasterbro Posts: 33Questions: 9Answers: 0

    I copied your sample code and only changed the extend to 'csvHtml5'. When I run the code it still exports the datatable columns that I set to display=none in the HTML.I am using version 1.10.09.

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

    Hi @measterbro ,

    We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. 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

This discussion has been closed.