Skip to content

Clipboard ​

Copy arbitrary text to the user's clipboard with the frontend event client->cs_event-clipboard_copy. The text travels as the first t_arg entry and is passed to the browser's navigator.clipboard.writeText API, which performs the copy entirely in the browser. An argument that is JSON — a bound structure or table passed as |${ client->_bind( ms_data ) }| — is copied as its JSON text, not as [object Object].

abap
CLASS zcl_sample_clipboard DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.
    DATA mv_text TYPE string.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_sample_clipboard IMPLEMENTATION.
  METHOD z2ui5_if_app~main.

    CASE abap_true.

      WHEN client->check_on_init( ).
        mv_text = `Hello from abap2UI5`.

        DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
            )->ele( n = `View` ns = `mvc`
                )->a( n = `xmlns`     v = `sap.m`
                )->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`

                )->ele( `Page`
                    )->tag( `Input`
                        )->a( n = `value` v = client->_bind( mv_text )
                    )->tag( `Button`
                        )->a( n = `text`  v = `copy to clipboard`
                        )->a( n = `press` v = client->_event( `COPY` ) ).

        client->view_display( view->stringify( ) ).

      WHEN client->check_on_event( `COPY` ).
        client->follow_up_action(
            val   = client->cs_event-clipboard_copy
            t_arg = VALUE #( ( mv_text ) ) ).
        client->message_toast_display( `copied` ).

    ENDCASE.

  ENDMETHOD.
ENDCLASS.

Copy the App State URL ​

To share the current app state instead of a custom string, compose the link with client->app_state_get_href( ) and hand it to the same clipboard_copy action — see App State.

WARNING

The browser's Clipboard API requires HTTPS (or localhost). On plain HTTP, and wherever writeText is refused, the frontend falls back to the older document.execCommand('copy'), which a browser allows only shortly after a user interaction — a copy that still fails is logged to the browser console, not reported to the user.

Local and Session Storage ​

The browser keeps a value across reloads and tabs in its localStorage, and for the life of one tab in its sessionStorage (sap.ui.util.Storage). abap2UI5 reaches both halves without JavaScript: the frontend action cs_event-store_data writes, the invisible custom control z2ui5:Storage reads.

The write takes one structure with the components type (local or session), prefix, key and value, handed over as the binding of that structure, so the frontend takes its current value:

abap
" PUBLIC SECTION
TYPES: BEGIN OF ty_s_storage,
         type   TYPE string,
         prefix TYPE string,
         key    TYPE string,
         value  TYPE string,
       END OF ty_s_storage.
DATA ms_storage TYPE ty_s_storage.

" in the handler
ms_storage = VALUE #( type = `local` prefix = `zcl_sample` key = `last_search` value = mv_search ).
client->follow_up_action( val   = client->cs_event-store_data
                          t_arg = VALUE #( ( |${ client->_bind( ms_storage ) }| ) ) ).

The ${ } spelling is what makes the same call work on a view wire, where a bare {/MS_STORAGE} would be read as an object literal and never fire; a handler resolves both. An empty value removes the key. value may also be a structure or a table — it is stored as JSON and comes back in the same shape.

The read side is the control, declared with xmlns:z2ui5="z2ui5.cc": it reads key under prefix from the store named by type after every rendering, writes a stored value into its bound value and fires finished when that value differs from what the binding holds — so one roundtrip per real change, and none while the stored and the bound value agree:

abap
)->tag( n = `Storage` ns = `z2ui5`
    )->a( n = `type`     v = `local`
    )->a( n = `prefix`   v = `zcl_sample`
    )->a( n = `key`      v = `last_search`
    )->a( n = `value`    v = client->_bind( mv_search )
    )->a( n = `finished` v = client->_event( `RESTORED` ) )

A key that holds nothing leaves the bound attribute alone. The storage belongs to the browser profile, not to the user who is logged on, so keep user data out of it and prefix every key with the app. For the events of an app that should survive a lost network connection rather than a value, see the offline queue.

Working Samples ​

Complete apps from the sample catalog that use what this page describes. Each is a single class in abap2UI5/samples unless its row names another of the three sample repositories — pull that repository with abapGit and start the class with ?app_start=<class>.

SampleClass
Copy to Clipboard (A)Z2UI5_CL_SMP_APP_325
Local and Session Storage (A,C)Z2UI5_CL_SMP_APP_327