Solution only copy selected cells with copy button

Solution only copy selected cells with copy button

nielsdevosnielsdevos Posts: 1Questions: 0Answers: 0

This is place for offer this solution for use the copy button on only selected cells for people like me who could not find this solution elsewhere and a place where people can discuss about a potential better solution.

 extend: 'copyHtml5',
                    text: 'copy visible fields',
                    exportOptions: {
                        columns: [0, ':visible']
                    }                           
                }, 
    {
    text: 'copy selected cell(s)',
    action: function (e, dt, button, config) {
        let selected = dt.cells('.selected');
        let values = selected.data().toArray();
        let count = selected.count();

        if (count === 0) {
            dt.buttons.info('copy selected cell(s)', 'No cells selected', 2000); // visible for 2s
            return;
        }

        // Building the text seperated by a tab
        let text = values.join('\t');

     
        $.fn.dataTable.ext.buttons.copyHtml5.action.call(this, e, dt, button, {
            ...config,
            customize: function () {
                return text; // only selected cells
            },
            info: false // standard message off
        });

        // Costum popup for 2s
        dt.buttons.info('copy selected cell(s)', count + ' cell' + (count === 1 ? '' : 'en') + ' copied', 2000);
    }

Replies

Sign In or Register to comment.