Buttons - Hide columns by default when exporting

Buttons - Hide columns by default when exporting

rkorebritsrkorebrits Posts: 8Questions: 1Answers: 0

Hi there,

I'm trying to find out if it's possible to hide columns by default, when exporting data via buttons. Preferably by setting classes or data attributes on the headings like below example of disabling sorting/ordering.

               settings.columnDefs = [
                    {
                        "targets": 'dt-nosort',
                        "orderable": false,
                    },
                ];

I know that there is the colvis option, but we don't want our users to have to disable certain columns before exporting, only to have to enable them again after exporting, to make use of the functionality in these columns. In our case we want to hide columns that e.g. contain action buttons on that row, so we can just show the actual data.

So something like this:

               settings.columnDefs = [
                    {
                        "targets": 'dt-no-export',
                        "button-export": false,
                    },
                ];

Is this possible?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,423Questions: 26Answers: 4,794
    Answer ✓
  • rkorebritsrkorebrits Posts: 8Questions: 1Answers: 0
    edited June 2017

    Thanks Kevin! Works like a charm, snippets of my solution using jQuery to select the columns:

    Before initialising:

                        options.buttons = [
                            buttonSettings($dataTable, 'csv'),
                            buttonSettings($dataTable, 'excel'),
                            buttonSettings($dataTable, 'pdf'),
                            buttonSettings($dataTable, 'copy')
                        ];
    

    The method:

        var buttonSettings = function ($dataTable, type) {
            var $columns = $('thead th:not(.dt-no-export)', $dataTable);
            
            return {
                extend: type + 'Html5',
                exportOptions: {
                    columns: $columns
                }
            };
        };
    

    Will have to add some $.extend logic later if needed

This discussion has been closed.