column options modified for export?

column options modified for export?

dknifedknife Posts: 14Questions: 4Answers: 1

I have my columns for buttons exports defined as arrays of pointers to the columns. These are combinations of visible and hidden columns. I'd like to be able to toggle a colvisGroup and in doing so, also remove those columns from being included in the export.

So I figured out how to use an action for the button on the colvisgroup to modify an array I'm using to store the column pointers.

{
                        extend: 'colvisGroup',
                        text: 'Volume',
                        hide: [18,19,20,21],
                        action: function(e, dt, node, config) {
                            var hidearray = [18,19,20,21];
                            agentExportCSV = agentExportCSV.filter(item => !hidearray.includes(item));
                            $.fn.dataTable.ext.buttons.colvisGroup.action.call(this, e, dt, node, config);
                        }
                    }

This correctly removes the columns it's hiding from the array and still executes the intended hiding action for the column group. However, no matter what I try for a callback on the columns definition on the CSV export, it seems to store that on initialization of dataTables rather than when executed.

{
                text: 'CSV',
                extend: 'csvHtml5',
                exportOptions: {
                    columns: function() {
                        return getExportColumns('agentExportCSV');
                    },
                    modifier: {
                        page: 'current'
                    }
                },
                filename: function() {
                    return getLocationName();
                }
            }

I've confirmed the columns array it's returning to the exportOptions columns is the new modified version but it always exports the full columns. Any way I get achieve what I need?

Answers

  • dknifedknife Posts: 14Questions: 4Answers: 1

    Can't see an edit for the post so just to add that doing a callback on columns: simply returning the array without the function results in all columns being included, not just the columns specified in the array.

  • dknifedknife Posts: 14Questions: 4Answers: 1

    Guess I should have read some more forum threads. This thread helped me understand the callback is for true/false per column rather than simply returning a defined array.

    https://datatables.net/forums/discussion/49017/exportoptions-columns

    So this code now correctly examines the current array of columns for the export and returns the required columns based on the colvisgroup modifications:

    columns: function(idx, data, node) {
                            return $.inArray(idx, agentExportCSV) >= 0;
                        }
    
This discussion has been closed.