Copy button behavior interrupted when using action option

Copy button behavior interrupted when using action option

lharkleroadlharkleroad Posts: 1Questions: 1Answers: 0

I am adding the copyHtml5 button to my table, and it all works great until I add the "action" option to my configuration. My goal here is to override the default confirmation message with my own which gets positioned in a shared message container used by my application. Here is the button configuration.

    buttons: [{
        text: 'Copy Table',
        extend: 'copyHtml5',
        exportOptions: {
            columns: ':visible',
            format: {
                body: function(innerHtml, col, row) {
                    var data = oTable.cell(row, col+':visible').data();
                    data = data === null ? '' : data;
                    return data;
                }
            }
        },
        action: function(e, dt, node, config) {
            var row_count = dt.rows().count();
            $.setStateMessage('Copied ' + row_count + ' rows to the clipboard', 'green');
        }
    }],

Again, without the action, the copy function works perfectly. With the action, the custom message is displayed, but nothing is copied to the clipboard. Just the presence of the action with no code inside the function causes the copy to break. Does anyone know why this might be happening? I am using DataTable 1.10.11 and Buttons 1.1.2. Thanks.

Answers

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    Again, without the action, the copy function works perfectly. With the action, the custom message is displayed, but nothing is copied to the clipboard

    Yup - that is expected. You are replacing the default action, which is to copy to the clipboard, with your own function!

    Two options:

    1. Call the default function from your own: $.fn.dataTable.ext.buttons.copyHtml5.action( e, dt, node, config );
    2. Implement your own copy to clipboard.

    Allan

This discussion has been closed.