Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Best practice: Copy String to browser clipboard

    Hi Isomorphic,

    I'd like to offer a button next to a FormItem that copies the FormItem's contents to the browser/system clipboard.

    I searched the forums for it and found this two old (2009 and 2012) posts.

    Are there any updates on this from your side or is JSNI still the only approach? If it has to be JSNI, do you have some best practices and Dos and Don'ts?

    Thank you & best regards,
    Blama

    #2
    The recommendation is unchanged - we recommend encouraging the end user to use keyboard shortcuts or menus rather than attempting to create a button that manipulates the native clipboard, as this is not reliably possible across browser, and any implementation that works now might be broken in the future as browsers change.

    Comment


      #3
      I understand the logic behind the recommendation. However, I see some behaviour that challenges the logic.

      Say there is a listgrid available, drag selecting some text across a few rows, then ctrl-c to copy, doesn't copy the selection to the system clipboard.

      However, using the browser edit menu - copy menu option in chrome/firefox does copy the selection to the system clipboard.

      If there is a way to programmatically mimic the browser edit menu copy option using a context menu on the grid or a 'copy cells' button, we could use such an approach to allow users to copy select grid text and paste into say an excel sheet. As against the grid to excel sample's which open a popup dialog with a text area containing the copied cells, which the user then has to copy once again and paste in the excel file.

      Can we mimic the browser edit menu copy option programmatically in SmartGWT ?

      Comment


        #4
        That's how it works for me.

        Tested on recent Safari, Firefox and Chrome

        Code:
        private static native boolean copyToClipboard() /*-{
            return $doc.execCommand('copy');
        }-*/;
        
        ...
        
        editField.addPickerIconClickHandler(new PickerIconClickHandler() {
        @Override
            public void onPickerIconClick(PickerIconClickEvent event) {
            editField.focusInItem();
            editField.selectValue();
            copyToClipboard();
        }
        });

        Comment

        Working...
        X