DataTables / Buttons

Buttons is an extension for DataTables that adds control buttons to the table.
Other
137 stars 149 forks source link

Fix bug when try customize the exported data #169

Closed enniosousa closed 1 year ago

enniosousa commented 4 years ago

I need remove duplicated rows that occurs when one or more columns are not displayed (ColVis) use the callback customizeData is the best way extending the button and using configuration, but it isn't overwrite the variable, I can only read the value

$.extend(true, $.fn.dataTable.ext.buttons, {
    separatedValues: {
        className: 'buttons-sv-export',
        text: '<i class="fa fa-file-excel-o"></i> separatedValues',
        exportData: {
            modifier: {
                selected: true
            },
            columns: ':visible'
        },
        exportOptions: {
            customizeData : function(data){
                var bodySeen = {};
                var bodyDistincted = [];
                var j = 0;
                for (var i = 0; i < data.body.length; i++) {
                    var item = data.body[i];
                    if (bodySeen[item] !== 1) {
                        bodySeen[item] = 1;
                        bodyDistincted[j++] = item;
                    }
                }

                data.body = bodyDistincted;

                return data;
            }
        },
        extend: 'csvHtml5',
    },
});
DataTables commented 4 years ago

What you are seeing is actually expected - it is expected to mutate the data rather than replace it. So for example you might use splice to remove an item from the array.

That said, I don't really any harm in this. Going to leave open for the moment to think about it and also because the docs would need to be updated.

AllanJard commented 1 year ago

Been looking at this again, and almost committed the change (although looking for an undefined return as well) but decided that I want to encourage anyone doing this to mutate the object and arrays rather than define their own, particularly with the new structure properties in the data.