Samples / Learn / Control Behaviour

Control Behaviour

Switches the page of a NavContainer and the tab of an IconTabBar by ID, so navigation inside a view costs no roundtrip.

Class
z2ui5_cl_smp_app_088
Repository
abap2UI5/samples
UI5 libraries
sap.m, sap.ui.core
Lowest UI5 release
1.71
In the browser
Runs in the playground
Controls it builds
sap.m.IconTabFilter, sap.m.IconTabHeader, sap.m.MessageStrip, sap.m.NavContainer, sap.m.Page, sap.m.Shell, sap.ui.core.mvc.View
Documentation
cookbook/event_navigation/frontend ↗

The class

" @keywords navcontainer icontabbar icontabheader page switch control_by_id whitelisted
" @summary Switches the page of a NavContainer and the tab of an IconTabBar by ID, so navigation inside a view costs no roundtrip.
" @docs https://abap2ui5.github.io/docs/cookbook/event_navigation/frontend
CLASS z2ui5_cl_smp_app_088 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    DATA mv_selected_key TYPE string.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS view_display.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_088 IMPLEMENTATION.

  METHOD z2ui5_if_app~main.

    me->client     = client.
    " no event branch: the tab switch is answered on the client by the
    " follow-up action below, nothing reaches the backend
    IF client->check_on_navigated( ).
      view_display( ).
    ENDIF.

  ENDMETHOD.


  METHOD view_display.

    " the NavContainer opens on the page of the selected tab: after a
    " re-display (Back, a restored state) the tab and the page stay in step
    DATA(initial_page) = COND string( WHEN mv_selected_key = `page2` OR mv_selected_key = `page3`
                                      THEN mv_selected_key
                                      ELSE `page1` ).

    DATA(page) = 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`
            )->ele( `Shell`
                )->ele( `Page`
                    )->a( n = `title`          v = `abap2UI5 - Control Behaviour - Switch NavContainer Page by ID`
                    )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
                    )->a( n = `navButtonPress` v = client->_event_nav_app_leave( )
                    )->ele( `content` ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `Selecting a tab in the IconTabHeader switches the NavContainer page on the client via the ` &&
                   `generic CONTROL_BY_ID front-end action (whitelisted method 'to'), without a backend roundtrip.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    page->ele( `IconTabHeader`
        )->a( n = `selectedKey` v = client->_bind( mv_selected_key )
        )->a( n = `select`      v = client->follow_up_action( val   = client->cs_event-control_by_id
                                                                                     t_arg = VALUE #( ( `NavCon` ) ( `to` ) ( `${$parameters>/key}` ) ) )
        )->a( n = `mode`        v = `Inline`
        )->ele( `items`
            )->ele( `IconTabFilter`
                )->a( n = `text` v = `Home`
                )->a( n = `key`  v = `page1`
            )->end(
            )->ele( `IconTabFilter`
                )->a( n = `text` v = `Applications`
                )->a( n = `key`  v = `page2`
            )->end(
            )->ele( `IconTabFilter`
                )->a( n = `text` v = `Users and Groups`
                )->a( n = `key`  v = `page3` ).

    page->ele( `NavContainer`
        )->a( n = `initialPage`           t = initial_page
        )->a( n = `id`                    v = `NavCon`
        )->a( n = `defaultTransitionName` v = `flip`
        )->ele( `pages`
            )->ele( `Page`
                )->a( n = `title` v = `first page`
                )->a( n = `id`    v = `page1`
            )->end(
            )->ele( `Page`
                )->a( n = `title` v = `second page`
                )->a( n = `id`    v = `page2`
            )->end(
            )->ele( `Page`
                )->a( n = `title` v = `third page`
                )->a( n = `id`    v = `page3` ).

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

  ENDMETHOD.

ENDCLASS.