Samples / Learn / Table

Table

The table selection modes - none, single and multi - side by side, and what each returns.

Class
z2ui5_cl_smp_app_019
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.Button, sap.m.Column, sap.m.ColumnListItem, sap.m.MessageStrip, sap.m.OverflowToolbar, sap.m.Page, sap.m.SegmentedButton, sap.m.SegmentedButtonItem, sap.m.Shell, sap.m.Table, sap.m.Text, sap.m.Title, sap.ui.core.mvc.View
Documentation
cookbook/model/tables ↗

The class

" @keywords selectionmode none single multi segmentedbutton checkbox
" @summary The table selection modes - none, single and multi - side by side, and what each returns.
" @docs https://abap2ui5.github.io/docs/cookbook/model/tables
CLASS z2ui5_cl_smp_app_019 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    TYPES:
      BEGIN OF ty_s_row,
        selkz TYPE abap_bool,
        title TYPE string,
        value TYPE string,
        descr TYPE string,
      END OF ty_s_row.
    TYPES ty_t_rows TYPE STANDARD TABLE OF ty_s_row WITH EMPTY KEY.
    DATA t_tab     TYPE ty_t_rows.
    DATA t_tab_sel TYPE ty_t_rows.
    DATA sel_mode  TYPE string.

  PROTECTED SECTION.
    DATA client TYPE REF TO z2ui5_if_client.

    METHODS view_display.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_019 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` ).
    DATA(page) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Table - Selection Modes: Single and Multi Select`
            )->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 = `Pick a selection mode with the SegmentedButton, select rows in the first table and ` &&
                   `press copy selected entries: each row's selected property is bound to its SELKZ field, so the ` &&
                   `selection is already in the internal table when the event arrives, and the second table shows it.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    page->ele( `SegmentedButton`
        )->a( n = `selectedKey`     v = client->_bind( sel_mode )
        )->a( n = `selectionChange` v = client->_event( `BUTTON_SEGMENT_CHANGE` )
        )->ele( `items`
            )->tag( `SegmentedButtonItem`
                )->a( n = `key`  v = `None`
                )->a( n = `text` v = `None`
            )->tag( `SegmentedButtonItem`
                )->a( n = `key`  v = `SingleSelect`
                )->a( n = `text` v = `SingleSelect`
            )->tag( `SegmentedButtonItem`
                )->a( n = `key`  v = `SingleSelectLeft`
                )->a( n = `text` v = `SingleSelectLeft`
            )->tag( `SegmentedButtonItem`
                )->a( n = `key`  v = `SingleSelectMaster`
                )->a( n = `text` v = `SingleSelectMaster`
            )->tag( `SegmentedButtonItem`
                )->a( n = `key`  v = `MultiSelect`
                )->a( n = `text` v = `MultiSelect` ).

    page->ele( `Table`
        )->a( n = `items`      v = client->_bind( t_tab )
        )->a( n = `headerText` v = `Table`
        )->a( n = `mode`       t = sel_mode
        )->ele( `columns`
            )->ele( `Column`
                )->tag( `Text`
                    )->a( n = `text` v = `Title`
            )->end(
            )->ele( `Column`
                )->tag( `Text`
                    )->a( n = `text` v = `Value`
            )->end(
            )->ele( `Column`
                )->tag( `Text`
                    )->a( n = `text` v = `Description`
            )->end(
        )->end(
        )->ele( `items`
            )->ele( `ColumnListItem`
                )->a( n = `selected` v = `{SELKZ}`
                )->ele( `cells`
                    )->tag( `Text`
                        )->a( n = `text` v = `{TITLE}`
                    )->tag( `Text`
                        )->a( n = `text` v = `{VALUE}`
                    )->tag( `Text`
                        )->a( n = `text` v = `{DESCR}` ).

    page->ele( `Table`
        )->a( n = `items` v = client->_bind( t_tab_sel )
        )->ele( `headerToolbar`
            )->ele( `OverflowToolbar`
                )->tag( `Title`
                    )->a( n = `text` v = `Selected Entries`
                )->tag( `Button`
                    )->a( n = `press` v = client->_event( `BUTTON_READ_SEL` )
                    )->a( n = `text`  v = `copy selected entries`
                    )->a( n = `icon`  v = `sap-icon://pull-down`
            )->end(
        )->end(
        )->ele( `columns`
            )->ele( `Column`
                )->tag( `Text`
                    )->a( n = `text` v = `Title`
            )->end(
            )->ele( `Column`
                )->tag( `Text`
                    )->a( n = `text` v = `Value`
            )->end(
            )->ele( `Column`
                )->tag( `Text`
                    )->a( n = `text` v = `Description`
            )->end(
        )->end(
        )->ele( `items`
            )->ele( `ColumnListItem`
                )->ele( `cells`
                    )->tag( `Text`
                        )->a( n = `text` v = `{TITLE}`
                    )->tag( `Text`
                        )->a( n = `text` v = `{VALUE}`
                    )->tag( `Text`
                        )->a( n = `text` v = `{DESCR}` ).

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

  ENDMETHOD.


  METHOD z2ui5_if_app~main.

    me->client = client.

    IF client->check_on_init( ).

      sel_mode = `None`.
      t_tab    = VALUE #( descr = `this is a description`
          ( title = `title_01` value = `value_01` )
          ( title = `title_02` value = `value_02` )
          ( title = `title_03` value = `value_03` )
          ( title = `title_04` value = `value_04` )
          ( title = `title_05` value = `value_05` ) ).
      view_display( ).

    ELSEIF client->check_on_navigated( ).
      view_display( ).
    ELSEIF client->check_on_event( `BUTTON_SEGMENT_CHANGE` ).

      client->message_toast_display( `Selection Mode changed` ).
      view_display( ).

    ELSEIF client->check_on_event( `BUTTON_READ_SEL` ).

      t_tab_sel = t_tab.
      DELETE t_tab_sel WHERE selkz <> abap_true.
      view_display( ).
    ENDIF.

  ENDMETHOD.

ENDCLASS.