Samples / Learn / Focus

Focus

Sets the focus into an Input and selects its text, so the next keystroke overwrites rather than appends.

Class
z2ui5_cl_smp_app_133
Repository
abap2UI5/samples
UI5 libraries
sap.m, sap.ui.core, sap.ui.layout
Lowest UI5 release
1.71
In the browser
Runs in the playground
Controls it builds
sap.m.Button, sap.m.Input, sap.m.Label, sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.Title, sap.ui.core.mvc.View, sap.ui.layout.form.SimpleForm
Documentation
cookbook/browser_interaction/focus ↗

The class

" @keywords cursor set_focus selection position textfield
" @summary Sets the focus into an Input and selects its text, so the next keystroke overwrites rather than appends.
" @docs https://abap2ui5.github.io/docs/cookbook/browser_interaction/focus
CLASS z2ui5_cl_smp_app_133 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    DATA field_01 TYPE string.
    DATA field_02 TYPE string.
    DATA selstart TYPE string.
    DATA selend   TYPE string.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS view_display.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_133 IMPLEMENTATION.


  METHOD view_display.

    DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
        )->ele( n = `View` ns = `mvc`
            )->a( n = `displayBlock` v = `true`
            )->a( n = `height`       v = `100%`
            )->a( n = `xmlns`        v = `sap.m`
            )->a( n = `xmlns:mvc`    v = `sap.ui.core.mvc`
            )->a( n = `xmlns:form`   v = `sap.ui.layout.form` ).

    DATA(page) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Focus - Set Focus and Select Text in an Input`
            )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
            )->a( n = `navButtonPress` v = client->_event_nav_app_leave( ) ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `Press focus here beside a field: the set_focus front-end action moves the keyboard ` &&
                   `focus into that input and selects the characters between Selection start and Selection end ` &&
                   `(0-based) - change the two positions and press again to move the selection.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    page->ele( n = `SimpleForm` ns = `form`
        )->a( n = `title`    v = `Focus & Cursor`
        )->a( n = `editable` b = abap_true
        )->ele( n = `content` ns = `form`
            )->tag( `Title`
                )->a( n = `text` v = `Input`
            )->tag( `Label`
                )->a( n = `text` v = `Selection start`
            )->tag( `Input`
                )->a( n = `value` v = client->_bind( selstart )
            )->tag( `Label`
                )->a( n = `text` v = `Selection end`
            )->tag( `Input`
                )->a( n = `value` v = client->_bind( selend )
            )->tag( `Label`
                )->a( n = `text` v = `Field 1`
            )->tag( `Input`
                )->a( n = `id`    v = `BUTTON01`
                )->a( n = `value` v = client->_bind( field_01 )
            )->tag( `Button`
                )->a( n = `press` v = client->_event( `BUTTON01` )
                )->a( n = `text`  v = `focus here`
            )->tag( `Label`
                )->a( n = `text` v = `Field 2`
            )->tag( `Input`
                )->a( n = `id`    v = `BUTTON02`
                )->a( n = `value` v = client->_bind( field_02 )
            )->tag( `Button`
                )->a( n = `press` v = client->_event( `BUTTON02` )
                )->a( n = `text`  v = `focus here` ).

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

  ENDMETHOD.


  METHOD z2ui5_if_app~main.

    me->client = client.
    IF client->check_on_init( ).

      field_01 = `this is a text`.
      field_02 = `this is another text`.
      selstart = `3`.
      selend   = `7`.
      view_display( ).

    ELSEIF client->check_on_navigated( ).
      view_display( ).
    ELSEIF client->check_on_event( `BUTTON01` ) OR client->check_on_event( `BUTTON02` ).

      client->follow_up_action(
          val   = z2ui5_if_client=>cs_event-set_focus
          t_arg = VALUE #( ( client->get_event( ) ) ( selstart ) ( selend ) ) ).
      client->message_toast_display( |focus changed| ).
    ENDIF.

  ENDMETHOD.
ENDCLASS.