How to hide all columns except for some and export to excel

How to hide all columns except for some and export to excel

Morrocs_ThiefMorrocs_Thief Posts: 5Questions: 1Answers: 0

Hey guys, I have a service which brings about 150 columns, but I need just to show to the user 5 of them.
I know that I can use something like this to hide columns, but I would like to know if there is the opposite way to hide/show.

               columns: columns,
            "columnDefs": [{
                "targets": -1,
                "data": null,
            },
            {
            "targets": [3, 4],
            "visible": false,
                "searchable": false
        },
               dom: 'Bfrtip',
            buttons: [{
                      text: 'Export to Excel',
                          extend: 'excelHtml5',
                          className: 'btn btn-primary',
                          exportOptions: {
                            columns: ':visible:not(:last-child)'
                          }
                  } ]  

Can I somehow hide ALL 150 columns but only target 5 of them?
Also, I need to add the export to excel button which shows 150 columns, if I hide 145 of 150, will they appear
in the excel ??
THANKS.

Answers

  • rf1234rf1234 Posts: 2,950Questions: 87Answers: 416

    https://datatables.net/reference/api/columns().visible()

    You could use exportOptions this way as well:

    exportOptions: {
        columns: function(column, data, node) {
            if (column > 15) { //exclude all columns greater 15
                return false;
            }
            return true;
        },
        modifier: { selected: null }, //make sure all rows show up (not only the filtered ones)
        ......
    
This discussion has been closed.