Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datatables: Custom copy-to-clipboard function

Tags:

datatables

The built-in copy-to-clipboard function in Datatables can copy the table head with the selected rows, so it pastes like this (Title, Number and Comment being columns):

Title Number Comment
Test   102    "nice"
Test2  103   "ok"

I need it like this:

Title: Test Number: 102 Comment: "nice"
Title: Test2 Number: 103 Comment: "ok"

My Datatables setup for the copy-button is currently like this:

dom: 'Bfrtip',
buttons: {
       buttons: [
         {
            extend: 'copyHtml5',
            text: 'Copy Selected Rows',
            header: false,
            exportOptions: {
                 modifier: {
                        selected: true
                 }
            }
         }
       ]
}

Is there a function to archive this? Or how can I modify the copying process?

like image 904
undefined Avatar asked May 29 '26 07:05

undefined


1 Answers

SOLUTION

You can use orthogonal option to specify data type copy requested for copy operations and columns.render to render appropriate content when data type copy is requested.

$('#example').DataTable({
    dom: 'Bfrtip',
    columnDefs: [{
        targets: "_all",
        render: function (data, type, full, meta) {
            if (type === 'copy') {
                var api = new $.fn.dataTable.Api(meta.settings);

                data = $(api.column(meta.col).header()).text() + ": " + data;
            }

            return data;
        }
    }],
    buttons: [{
        extend: 'copyHtml5',
        text: 'Copy Selected Rows',
        header: false,
        exportOptions: {
            modifier: {
                selected: true
            },
            orthogonal: 'copy'
        }
    }]
});

DEMO

See this jsFiddle for code and demonstration.

like image 86
Gyrocode.com Avatar answered Jun 04 '26 22:06

Gyrocode.com



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!