Copy to clipboard from Flexgrid by clicking on a button (not ctrl+c)

Posted by: luca.mannillo on 30 March 2020, 3:42 pm EST

  • Posted 30 March 2020, 3:42 pm EST

    Hi all,

    I’ve read very carefully the Clipboard class documentation

    https://www.grapecity.com/wijmo/api/classes/wijmo.clipboard.html

    so I got aware that the Copy method works only after user press a clipboard copy command (such as ctrl+c).

    My goal is to create an external “Copy to clipboard” button, so on the click I’ve

    1. set the active element from the button to one cell of the selection (like if the user has pressed ctrl+c on it)
    2. dispatched a synthetic event for the ctrl+c (programmatically)

    Now let’s refer to this part of the code (wijmo.js)

    e._copyPaste = function (e, n)
            {
                for (var r = t.getActiveElement(), i = t.closest(r, ".wj-control"); i && t.Control.getControl(i); )
                {
                    debugger;
                    i = i.parentElement;
                }
    
                debugger;
                if (i = i || document.body)
                {
                    var o = t.createElement('<textarea class="wj-clipboard" style="position:fixed;opacity:0"/>', i);
                    t.isString(e) && (o.value = e), o.select(), o.onkeydown = function (t)
                    {
                        t.preventDefault()
                    }, setTimeout(function ()
                    {
                        r.focus(), t.removeChild(o), t.isFunction(n) && n(o.value)
                    }, t.Control._CLIPBOARD_DELAY)
                }
            }
    

    Variables and behavior seem the same by comparing “crtl+c” and “synthetic ctrl+c” events but the content is still not copied to the clipboard in this second case. Any help?

    Thank you in advance,

    Luca.

  • Posted 31 March 2020, 4:12 pm EST

    Hi Luca,

    The copy event can be used after a user event like pressing the copy shortcut keys and also a button click. In the copy button’s event handler, simply use the getClipString method of FlexGrid to get the clip string of the selection and use the copy method to copy it to the clipboard:

    
    var text = grid.getClipString();
    wjcCore.Clipboard.copy(text);
    
    

    You may also refer to the sample link below for reference:

    https://stackblitz.com/edit/js-uen3gu

    Regards,

    Ashwin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels