Solution only copy selected cells with copy button
Solution only copy selected cells with copy button

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
You can use the
selector-modifier
{selected: true}
in place of'.selected'
. I built a running test case with your code and using{selected: true}
to show how your demo works:https://live.datatables.net/valajowe/1/edit
Kevin