[ABAP] SALV: Eventhandler für Auswahl einer Zeile (Row) bzw. Spalte (Col)

* Eventhandler
CLASS lcl_events DEFINITION.
  PUBLIC SECTION.

* Platzhalter für Referenz auf SALV-Grid
    CLASS-DATA: o_salv TYPE REF TO cl_salv_table.
* Platzhalter für Referenz auf interne Tabelle
    CLASS-DATA: it_t024 TYPE STANDARD TABLE OF t024 WITH DEFAULT KEY.

* Eventhandler-Methode für Change-Select, Verzögerung ~1.5s
    CLASS-METHODS: on_row_select FOR EVENT delayed_changed_sel_callback OF cl_gui_alv_grid
      IMPORTING sender.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.
* Event behandelt die Auswahl Zeilen/Spalten im Gitter mit 1,5s Verzögerung
  METHOD on_row_select.
    TRY.
* referenz auf ALV-Grid holen
        DATA(o_alv) = CAST cl_gui_alv_grid( sender ).

* selektierte Zeilen, abh. vom if_salv_c_selection_mode
        o_alv->get_selected_rows( IMPORTING et_row_no     = DATA(it_sel_rows)
                                            et_index_rows = DATA(it_sel_rows_idx) ).

* selektierte Spalten, abh. vom if_salv_c_selection_mode
        o_alv->get_selected_columns( IMPORTING et_index_columns = DATA(it_sel_cols_idx) ).

        LOOP AT it_sel_cols_idx ASSIGNING FIELD-SYMBOL(<col>).
        ENDLOOP.

        LOOP AT it_sel_rows_idx ASSIGNING FIELD-SYMBOL(<row>).
        ENDLOOP.

      CATCH cx_root.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_salv_helper DEFINITION INHERITING FROM cl_salv_model_base FINAL.
  PUBLIC SECTION.

    CLASS-METHODS:
      get_alv_from_salv
        IMPORTING
          io_salv_grid   TYPE REF TO cl_salv_table
        RETURNING
          VALUE(ro_grid) TYPE REF TO cl_gui_alv_grid.
ENDCLASS.

CLASS lcl_salv_helper IMPLEMENTATION.
*---------------------------------------------------------------------*
* Wandelt eine Referenz auf cl_salv_table in ein cl_gui_alv_grid um.
*---------------------------------------------------------------------*
* -> io_model - Referenz auf cl_salv_table
* <- ro_grid  - Referenz auf cl_gui_alv_grid, bei Fehler INITIAL
*---------------------------------------------------------------------*
  METHOD get_alv_from_salv.

    IF io_salv_grid IS BOUND.
      TRY.
          DATA(o_model) = CAST cl_salv_model_base( io_salv_grid->extended_grid_api( ) ).

* Funktioniert nur, wenn SALV-Table schon mit display( ) angezeigt und
* somit schon ein Container dafür erzeugt wurde. Andernfalls ist r_adapter INITIAL.
          IF o_model->r_controller IS BOUND AND o_model->r_controller->r_adapter IS BOUND.

            IF o_model->r_controller->r_adapter IS INSTANCE OF if_salv_table_display_adapter.
              ro_grid = CAST if_salv_table_display_adapter( o_model->r_controller->r_adapter )->get_grid( ).
            ENDIF.

          ENDIF.
        CATCH cx_root.
      ENDTRY.
    ENDIF.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  TRY.
* Beispieldaten (Einkäufergruppen) holen
      SELECT FROM t024
        FIELDS *
        INTO TABLE @lcl_events=>it_t024
        UP TO 10 ROWS.

      IF sy-subrc = 0.
        cl_salv_table=>factory( EXPORTING r_container  = cl_gui_container=>default_screen
                                IMPORTING r_salv_table = lcl_events=>o_salv
                                CHANGING  t_table      = lcl_events=>it_t024 ).

* Standardbuttons der SALV-Table
        lcl_events=>o_salv->get_functions( )->set_all( abap_true ).

* if_salv_c_selection_mode=>single    : es kann eine Zeile im SALV/ALV selektiert werden
* if_salv_c_selection_mode=>row_column: es können mehrere Zeile und Spalten im SALV/ALV selektiert werden
* nach 1,5s wird dann der Event on_delayed_change_select ausgelöst
        lcl_events=>o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>single ).

* SALV anzeigen
        lcl_events=>o_salv->display( ).

* ALV-Objektreferenz aus dem SALV-Grid holen, funktioniert erst nach o_salv->display( )
        DATA(o_alv) = lcl_salv_helper=>get_alv_from_salv( lcl_events=>o_salv ).
        IF o_alv IS BOUND.
* Event-Handler des ALV
          SET HANDLER lcl_events=>on_row_select FOR o_alv.
* Event registrieren
          o_alv->register_delayed_event( i_event_id = cl_gui_alv_grid=>mc_evt_delayed_change_select ).
        ENDIF.

* Toolbar der Listausgabe unterdrücken
        cl_abap_list_layout=>suppress_toolbar( ).

* Listausgabe erzwingen für Erzeugung von cl_gui_container=>default_screen
        WRITE: space.

      ENDIF.
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] GUI-Controls stapeln

* Quelle: https://www.tricktresor.de/blog/controls-stapeln/

DATA: it_mara TYPE STANDARD TABLE OF mara WITH DEFAULT KEY.
DATA: o_dock TYPE REF TO cl_gui_docking_container.
DATA: o_txt TYPE REF TO cl_gui_textedit.
DATA: o_alv TYPE REF TO cl_gui_alv_grid.

PARAMETERS: rb_txt RADIOBUTTON GROUP rbg DEFAULT 'X' USER-COMMAND rbc.
PARAMETERS: rb_alv RADIOBUTTON GROUP rbg.

INITIALIZATION.
  IF NOT o_dock IS BOUND.
* Containerobjekt erzeugen
    o_dock = NEW #( side  = cl_gui_docking_container=>dock_at_bottom
                    ratio = 90 ).

* Texteditor erzeugen
    o_txt = NEW #( parent = o_dock ).

* Daten für ALV holen
    SELECT *
      INTO TABLE @it_mara
      FROM mara
      UP TO 100 ROWS.

* ALV-Gitter erzeugen
    o_alv = NEW #( i_parent = o_dock ).
    o_alv->set_table_for_first_display( EXPORTING
                                          i_structure_name = 'MARA'
                                        CHANGING
                                          it_outtab = it_mara ).
  ENDIF.

AT SELECTION-SCREEN.
* wenn Radiobuttons geklickt
  IF sy-ucomm = 'RBC'.
* je nach Radiobutton die GUI-Controls ein-/ausblenden
    CASE abap_true.
      WHEN rb_txt.
        o_txt->set_visible( abap_true ).
        o_alv->set_visible( abap_false ).
      WHEN rb_alv.
        o_txt->set_visible( abap_false ).
        o_alv->set_visible( abap_true ).
    ENDCASE.
  ENDIF.

[ABAP] ALV-Grid: Dropdown-Liste verwenden

**********************************************************************
*
* Variablen
*
**********************************************************************
DATA: gv_screen_status TYPE string VALUE 'INIT'.
DATA: gv_carrid TYPE spfli-carrid.
DATA: gv_connid TYPE spfli-connid.
DATA: o_alv TYPE REF TO cl_gui_alv_grid.
DATA: it_spfli TYPE STANDARD TABLE OF spfli WITH DEFAULT KEY.
**********************************************************************
*
* leeres Dynpro als Dummy für ALV-Grid
*
**********************************************************************
SELECTION-SCREEN BEGIN OF SCREEN 2000.
SELECTION-SCREEN END OF SCREEN 2000.
**********************************************************************
*
* SELECTION-SCREEN
*
**********************************************************************
SELECT-OPTIONS: so_carr FOR gv_carrid.
SELECT-OPTIONS: so_conn FOR gv_connid.
**********************************************************************
*
* Eventhandler
*
**********************************************************************
CLASS lcl_events DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:
      on_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING
            e_object
            e_interactive
            sender.

    CLASS-METHODS:
      on_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
            er_data_changed
            sender.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.

  METHOD on_data_changed.
* geänderte Zellen durchgehen
    LOOP AT er_data_changed->mt_good_cells ASSIGNING FIELD-SYMBOL(<fs_changed>).
      IF <fs_changed> IS ASSIGNED.
* Zeile x aus der iTab it_mara rausholen und daraus die Zelle anhand des Spaltennamens (Feldnamens) holen
        ASSIGN COMPONENT <fs_changed>-fieldname OF STRUCTURE it_spfli[ <fs_changed>-row_id ] TO FIELD-SYMBOL(<fs_mara_field>).

* Änderungswert in die Zelle der iTab (it_mara) rückschreiben
        <fs_mara_field> = <fs_changed>-value.
      ENDIF.

    ENDLOOP.

* DB Update
    FIELD-SYMBOLS: <fs_tab> TYPE table.
    FIELD-SYMBOLS: <fs_row> TYPE spfli.

    ASSIGN er_data_changed->mp_mod_rows->* TO <fs_tab>.

    LOOP AT <fs_tab> ASSIGNING <fs_row>.
* DB Update hier
    ENDLOOP.

  ENDMETHOD.

  METHOD on_toolbar.
* alle Buttons entfernen, bis auf folgende:
    DELETE e_object->mt_toolbar WHERE
        function NE cl_gui_alv_grid=>mc_fc_refresh          " Refresh
    AND function NE cl_gui_alv_grid=>mc_mb_export           " Excel
    AND function NE cl_gui_alv_grid=>mc_fc_current_variant. " Layout

  ENDMETHOD.
ENDCLASS.
**********************************************************************
*
* INITIALIZATION
*
**********************************************************************
INITIALIZATION.

* Vorbelegungen für Selektionsbild
  so_carr[] = VALUE #( ( sign = 'I' option = 'EQ' low = 'LH' ) ).

**********************************************************************
*
* AT SELECTION-SCREEN OUTPUT
*
**********************************************************************
AT SELECTION-SCREEN OUTPUT.

* Wenn vorher das Selektionsbild 1000 angezeigt wurde
  IF gv_screen_status = 'IN_SELECTION'.
* Daten holen
    SELECT * FROM spfli INTO TABLE @it_spfli
       WHERE carrid IN @so_carr
         AND connid IN @so_conn.
* ALV-Gitter anzeigen
    o_alv = NEW #( i_parent      = cl_gui_container=>default_screen
                   i_appl_events = abap_true ).

* Eventhandler registrieren
    SET HANDLER lcl_events=>on_toolbar FOR o_alv.
    SET HANDLER lcl_events=>on_data_changed FOR o_alv.

* Ereignisse registrieren
    o_alv->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_enter ).
    o_alv->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_modified ).

* ALV-Grid selektionsbereit setzen
    o_alv->set_ready_for_input( i_ready_for_input = 1 ).

* Layout des ALV setzen
    DATA(lv_layout) = VALUE lvc_s_layo( zebra      = abap_true
                                        cwidth_opt = 'A'
                                        grid_title = 'Flugverbindungen' ).

* Feldkatalog automatisch durch SALV erstellen lassen
    DATA: o_salv TYPE REF TO cl_salv_table.

    cl_salv_table=>factory( IMPORTING
                              r_salv_table = o_salv
                            CHANGING
                              t_table      = it_spfli ).

    DATA(it_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog( r_columns      = o_salv->get_columns( )
                                                                       r_aggregations = o_salv->get_aggregations( ) ).

* Drop-Down-Liste definieren und an das ALV-Gitter übergeben
    DATA(it_dropdown) = VALUE lvc_t_drop( ( handle = 1
                                            value  = 'BERLIN' )
                                          ( handle = 1
                                            value  = 'FRANKFURT' )
                                          ( handle = 1
                                            value  = 'NEW YORK' ) ).

    o_alv->set_drop_down_table( it_drop_down = it_dropdown ).

* im Feldkatalog alle Zellen der Spalte "CITYFROM" des ALV-Grids auf
* editierbar stellen, die restlichen Zellen sind nicht editierbar
    LOOP AT it_fcat ASSIGNING FIELD-SYMBOL(<fcat>).
      CASE <fcat>-fieldname.
        WHEN 'CITYFROM'.
          <fcat>-edit = abap_true.
          <fcat>-drdn_hndl = 1.      " Drop-Down-Liste mit Handle = 1 für die Zelle setzen
          <fcat>-outputlen = 10.
        WHEN OTHERS.
          <fcat>-edit = abap_false.
      ENDCASE.
    ENDLOOP.

* ALV anzeigen
    o_alv->set_table_for_first_display( EXPORTING
                                          i_bypassing_buffer = abap_false
                                          i_save             = 'A'
                                          is_layout          = lv_layout
                                        CHANGING
                                          it_fieldcatalog    = it_fcat
                                          it_outtab          = it_spfli ).

* Focus auf ALV setzen
    cl_gui_alv_grid=>set_focus( control = o_alv ).

* leere SAP-Toolbar ausblenden
    cl_abap_list_layout=>suppress_toolbar( ).

* Flag für Screen-Status auf ALV-Anzeige setzen
    gv_screen_status = 'IN_ALV'.
  ENDIF.
**********************************************************************
*
* START-OF-SELECTION
*
**********************************************************************
START-OF-SELECTION.

* Wir befinden uns im Anzeigebereich des Selektionsbildes
  gv_screen_status = 'IN_SELECTION'.

* Trick: leeren Dummy-Screen 2000 anzeigen und intern für das ALV-Grid in
* AT SELECTION-SCREEN OUTPUT als cl_gui_container=>default_screen nutzen
  CALL SELECTION-SCREEN 2000.

[ABAP] ALV-Grid: Zugriff auf geänderte Tabellenzeilen

CLASS lcl_events DEFINITION.
  PUBLIC SECTION.
    METHODS:
      on_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
            er_data_changed
            sender.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.
  METHOD on_data_changed.

    FIELD-SYMBOLS: <fs_tab> TYPE table.
    FIELD-SYMBOLS: <fs_row> TYPE spfli.

* Datenreferenz auf Feldsymbol übergeben
    ASSIGN er_data_changed->mp_mod_rows->* TO <fs_tab>.

* geänderte Zeilen durchloopen
    LOOP AT <fs_tab> ASSIGNING <fs_row>.
      ...
    ENDLOOP.

  ENDMETHOD.
ENDCLASS.

[ABAP] ALV-Grid: Auf Events der Standard-Toolbar-Buttons reagieren (z.B. Refresh)

* Standard Toolbar-Button ersetzen
METHOD on_toolbar.
* Liste der Standardbuttons durchgehen und den gewünschten Button suchen
* in diesem Beispiel den Refresh-Button
  LOOP AT e_object->mt_toolbar ASSIGNING FIELD-SYMBOL(<fs_button>) WHERE ( function = cl_gui_alv_grid=>mc_fc_refresh ).
* mit neuem USER-Command setzen, damit bei Button-Klick on_user_command getriggert wird
    <fs_button>-function = 'BTN_REFRESH'.
  ENDLOOP.
ENDMETHOD.

* on_user_command standardmäßig nur von nachträglich zur Toolbar hinzugefügten
* User-Buttons oder geänderten Standard-Buttons getriggert
METHOD on_user_command.
* wenn BTN_REFRESH geklickt
  IF e_ucomm = 'BTN_REFRESH'.
    ...
  ENDIF.
ENDMETHOD.

[ABAP] ALV-Grid als Property-Grid (Parametertabelle) mit Edit-Feldern und Typprüfung

Quelle / Inspiration gefunden auf: www.tricktresor.de

* ALV-Konstanten
INCLUDE <cl_alv_control>.

CLASS lcl_main DEFINITION.
  PUBLIC SECTION.

    TYPES: ty_enum TYPE i.

    TYPES: BEGIN OF ty_param,
             name  TYPE string,
             type  TYPE ty_enum,
             text  TYPE string,
             value TYPE string,
           END OF ty_param.

    TYPES: ty_it_params TYPE STANDARD TABLE OF ty_param WITH NON-UNIQUE DEFAULT KEY.

* Enum
    CLASS-DATA: string TYPE ty_enum VALUE 1 READ-ONLY.
    CLASS-DATA: int TYPE ty_enum VALUE 2 READ-ONLY.
    CLASS-DATA: float TYPE ty_enum VALUE 3 READ-ONLY.
    CLASS-DATA: date TYPE ty_enum VALUE 4 READ-ONLY.

* Bezeichner
    CONSTANTS: co_name TYPE string VALUE 'NAME'.
    CONSTANTS: co_type TYPE string VALUE 'TYPE'.
    CONSTANTS: co_text TYPE string VALUE 'TEXT'.
    CONSTANTS: co_value TYPE string VALUE 'VALUE'.

    METHODS: constructor
      IMPORTING
        o_parent TYPE REF TO cl_gui_container.

    METHODS: init_grid.

    METHODS: add_parameter
      IMPORTING
        parameter TYPE ty_param.

    METHODS: get_params
      RETURNING VALUE(rv_it_parameters) TYPE ty_it_params.

  PROTECTED SECTION.

    DATA: o_grid TYPE REF TO cl_gui_alv_grid.
    DATA: it_params TYPE ty_it_params.

    METHODS: on_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
      IMPORTING
          er_data_changed
          sender.

ENDCLASS.

CLASS lcl_main IMPLEMENTATION.

  METHOD constructor.
    o_grid = NEW #( i_parent = o_parent ).
  ENDMETHOD.

* ALV-Gitter initialisieren
  METHOD init_grid.

    DATA(it_fieldcat) = VALUE lvc_t_fcat( ( fieldname  = co_name
                                            outputlen  = 15
                                            coltext    = 'Parameter'
                                            style      = alv_style_font_bold + alv_style_color_int_group )
                                          ( fieldname  = co_type
                                            outputlen  = 5
                                            coltext    = 'Type'
                                            no_out     = abap_true )
                                          ( fieldname  = co_text
                                            outputlen  = 15
                                            coltext    = 'Text' )
                                          ( fieldname  = co_value
                                            outputlen  = 20
                                            coltext    = 'Value'
                                            edit       = abap_true
                                            fix_column = abap_true ) ).

    DATA(s_layout) = VALUE lvc_s_layo( no_toolbar = abap_true
                                       no_headers = abap_false ).

    SET HANDLER on_data_changed FOR o_grid.

* Tastenevents registrieren
    o_grid->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_enter ).    " Enter
    o_grid->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_modified ). " Eingabe, Cursortasten

* ALV-Grid selektionsbereit setzen
    o_grid->set_ready_for_input( i_ready_for_input = 1 ).

    o_grid->set_table_for_first_display( EXPORTING
                                           is_layout       = s_layout
                                         CHANGING
                                           it_outtab       = it_params
                                           it_fieldcatalog = it_fieldcat ).

  ENDMETHOD.

* Parameterliste zurückholen
  METHOD get_params.
    rv_it_parameters = it_params.
  ENDMETHOD.

* Parameter hinzufügen
  METHOD add_parameter.
    APPEND VALUE #( name  = parameter-name
                    type  = parameter-type
                    text  = parameter-text
                    value = parameter-value ) TO it_params.
  ENDMETHOD.

  METHOD on_data_changed.
* geänderte Zellen durchgehen
    LOOP AT er_data_changed->mt_good_cells ASSIGNING FIELD-SYMBOL(<fs_changed>).
      IF <fs_changed> IS ASSIGNED.
* Zeile x aus der iTab it_params rausholen und daraus die Zelle anhand des Spaltennamens (Feldnamens) holen
        TRY.
            ASSIGN COMPONENT <fs_changed>-fieldname OF STRUCTURE it_params[ <fs_changed>-row_id ] TO FIELD-SYMBOL(<fs_param>).

            IF <fs_param> IS ASSIGNED.
* Änderungswert in die Zelle der iTab (it_params) rückschreiben
              CASE it_params[ <fs_changed>-row_id ]-type.
                WHEN lcl_main=>string.
                  <fs_param> = <fs_changed>-value.
                WHEN lcl_main=>int.
* Typprüfung für Int
                  IF cl_abap_matcher=>create( pattern = '^[-+]?[0-9]*$'
                                              text = <fs_changed>-value
                                              ignore_case = abap_true )->match( ) = abap_true.

                    <fs_param> = <fs_changed>-value.
                  ELSE.
* Eingabefehler im Protokoll anzeigen
                    er_data_changed->add_protocol_entry( i_msgid     = '0K'
                                                         i_msgno     = '000'
                                                         i_msgty     = 'E'
                                                         i_msgv1     = 'Ungültiger Wert:'
                                                         i_msgv2     = <fs_changed>-value
                                                         i_msgv3     = 'Bitte einen Wert vom Typ int eingeben!'
                                                         i_msgv4     = ''
                                                         i_fieldname = <fs_changed>-fieldname
                                                         i_row_id    = <fs_changed>-row_id ).
                  ENDIF.
                WHEN lcl_main=>float.
* Typprüfung für Float
                  IF cl_abap_matcher=>create( pattern = '^[-+]?[0-9]*[,]?[0-9]+([eE][-+]?[0-9]+)?$'
                                              text = <fs_changed>-value
                                              ignore_case = abap_true )->match( ) = abap_true.

                    <fs_param> = <fs_changed>-value.
                  ELSE.
* Eingabefehler im Protokoll anzeigen
                    er_data_changed->add_protocol_entry( i_msgid     = '0K'
                                                         i_msgno     = '000'
                                                         i_msgty     = 'E'
                                                         i_msgv1     = 'Ungültiger Wert:'
                                                         i_msgv2     = <fs_changed>-value
                                                         i_msgv3     = 'Bitte einen Wert vom Typ float eingeben!'
                                                         i_msgv4     = ''
                                                         i_fieldname = <fs_changed>-fieldname
                                                         i_row_id    = <fs_changed>-row_id ).
                  ENDIF.
                WHEN lcl_main=>date.
* Typprüfung für Date
                  IF cl_abap_matcher=>create( pattern = '^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$'
                                              text = <fs_changed>-value
                                              ignore_case = abap_true )->match( ) = abap_true.

                    <fs_param> = <fs_changed>-value.
                  ELSE.
* Eingabefehler im Protokoll anzeigen
                    er_data_changed->add_protocol_entry( i_msgid     = '0K'
                                                         i_msgno     = '000'
                                                         i_msgty     = 'E'
                                                         i_msgv1     = 'Ungültiger Wert:'
                                                         i_msgv2     = <fs_changed>-value
                                                         i_msgv3     = 'Bitte einen Datumswert im Format dd.mm.yyyy eingeben!'
                                                         i_msgv4     = ''
                                                         i_fieldname = <fs_changed>-fieldname
                                                         i_row_id    = <fs_changed>-row_id ).
                  ENDIF.
              ENDCASE.
            ENDIF.
          CATCH cx_root.
        ENDTRY.
      ENDIF.
    ENDLOOP.

  ENDMETHOD.

ENDCLASS.

**********************************************************************
*
* Datentypen, Variablen, Konstanten
*
**********************************************************************
DATA: o_main TYPE REF TO lcl_main.

**********************************************************************
*
* SELECTION-SCREEN
*
**********************************************************************
SELECTION-SCREEN BEGIN OF SCREEN 2000.
SELECTION-SCREEN END OF SCREEN 2000.

**********************************************************************
*
* INITIALIZATION
*
**********************************************************************
INITIALIZATION.

  o_main = NEW #( o_parent = NEW cl_gui_docking_container( side = cl_gui_docking_container=>dock_at_left
                                                           extension = 500
                                                           no_autodef_progid_dynnr = abap_true ) ).

* Beispiel-Parameter einfügen
* String-Parameter
  o_main->add_parameter( VALUE #( name = 'PERSON_VOR'
                                  type = lcl_main=>string
                                  text = 'Vorname'
                                  value = 'Udo' ) ).
* String-Parameter
  o_main->add_parameter( VALUE #( name = 'PERSON_NACH'
                                  type = lcl_main=>string
                                  text = 'Nachname'
                                  value = 'Lehmann' ) ).
* Int-Parameter
  o_main->add_parameter( VALUE #( name = 'PERSON_ALTER'
                                  type = lcl_main=>int
                                  text = 'Alter'
                                  value = '34' ) ).
* Float-Parameter
  o_main->add_parameter( VALUE #( name = 'PERSON_GROESSE'
                                  type = lcl_main=>float
                                  text = 'Größe'
                                  value = '1,80' ) ).
* Date-Parameter
  o_main->add_parameter( VALUE #( name = 'PERSON_DATE'
                                  type = lcl_main=>date
                                  text = 'Geburtsdatum'
                                  value = '01.01.1980' ) ).

**********************************************************************
*
* AT SELECTION-SCREEN OUTPUT
*
**********************************************************************
AT SELECTION-SCREEN OUTPUT.

  IF o_main IS BOUND.
    o_main->init_grid( ).
  ENDIF.

**********************************************************************
*
* AT SELECTION-SCREEN
*
**********************************************************************
AT SELECTION-SCREEN.
* wenn "Ausführen" (F8) geklickt wurde
  IF sy-ucomm = 'CRET'.
    cl_demo_output=>display_data( o_main->get_params( ) ).
  ENDIF.

**********************************************************************
*
* START-OF-SELECTION
*
**********************************************************************
START-OF-SELECTION.
* leeres Selektionbild 2000 anzeigen
  CALL SELECTION-SCREEN 2000.

[ABAP] ALV-Grid als Property-Grid mit Tri-State Klickfeldern/Buttons (ON / OFF / UNDEF)

Quelle / Inspiration gefunden auf: www.tricktresor.de

Die Codeteile mit UNDEF / abap_undefined können auch auskommentiert werden, so dass sich eine Möglichkeit der Dual-State Umschaltung (ON / OFF) per Mausklick ergibt.

* ALV-Konstanten
INCLUDE <cl_alv_control>.

CLASS lcl_main DEFINITION.
  PUBLIC SECTION.

    TYPES: BEGIN OF ty_param,
             name   TYPE string,
             text   TYPE string,
             status TYPE boolean,
           END OF ty_param.

    TYPES: ty_it_params TYPE STANDARD TABLE OF ty_param WITH NON-UNIQUE DEFAULT KEY.

    TYPES: BEGIN OF ty_ui_param,
             name    TYPE string,
             text    TYPE string,
             status  TYPE icon_text,
             t_color TYPE lvc_t_scol,
             t_style TYPE lvc_t_styl,
           END OF ty_ui_param.

    TYPES: ty_it_ui_params TYPE STANDARD TABLE OF ty_ui_param.

    TYPES: ty_cell_type TYPE i.

    CONSTANTS: co_celltype_hotspot TYPE ty_cell_type VALUE 1.
    CONSTANTS: co_celltype_button TYPE ty_cell_type VALUE 2.

* Farben
    CONSTANTS: co_color_on TYPE i VALUE col_positive.
    CONSTANTS: co_color_off TYPE i VALUE col_negative.
    CONSTANTS: co_color_undef TYPE i VALUE col_total.

* Bezeichner
    CONSTANTS: co_status TYPE string VALUE 'STATUS'.
    CONSTANTS: co_on TYPE string VALUE 'ON'.
    CONSTANTS: co_off TYPE string VALUE 'OFF'.
    CONSTANTS: co_undef TYPE string VALUE 'N/A'.

* Icons
    CONSTANTS status_icon_on TYPE icon_text VALUE icon_oo_object.
    CONSTANTS status_icon_off TYPE icon_text VALUE icon_system_start_recording.
    CONSTANTS status_icon_undef TYPE icon_text VALUE icon_oo_class.
*    CONSTANTS status_icon_undef TYPE icon_text VALUE icon_led_yellow.

    METHODS: constructor
      IMPORTING
        o_parent TYPE REF TO cl_gui_container.

    METHODS: init_grid
      IMPORTING
        cell_type TYPE ty_cell_type.

    METHODS: add_parameter
      IMPORTING
        parameter TYPE ty_param.

    METHODS: get_params
      RETURNING VALUE(rv_it_parameters) TYPE ty_it_params.

  PROTECTED SECTION.

    DATA: o_grid TYPE REF TO cl_gui_alv_grid.
    DATA: it_params TYPE ty_it_params.
    DATA: it_ui_params TYPE ty_it_ui_params.

    METHODS: set_color
      IMPORTING
                status       TYPE boolean
      RETURNING VALUE(color) TYPE lvc_t_scol.

    METHODS: on_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid
      IMPORTING
          e_row_id.

    METHODS: on_button_click FOR EVENT button_click OF cl_gui_alv_grid
      IMPORTING
          es_col_id
          es_row_no.

ENDCLASS.

CLASS lcl_main IMPLEMENTATION.

  METHOD constructor.
    o_grid = NEW #( i_parent = o_parent ).
  ENDMETHOD.

  METHOD init_grid.

    CLEAR: it_ui_params.

* Parameter in ALV-Zeilen umwandeln
    LOOP AT it_params ASSIGNING FIELD-SYMBOL(<fs_param>).

      CASE <fs_param>-status.
        WHEN abap_true.
* ON
          APPEND VALUE #( name = <fs_param>-name
                          text = <fs_param>-text
                          status = |{ status_icon_on } { co_on }|
                          t_color = me->set_color( abap_true ) ) TO it_ui_params.
        WHEN abap_false.
* OFF
          APPEND VALUE #( name = <fs_param>-name
                          text = <fs_param>-text
                          status = |{ status_icon_off } { co_off }|
                          t_color = me->set_color( abap_false ) ) TO it_ui_params.
        WHEN abap_undefined.
* UNDEF
          APPEND VALUE #( name = <fs_param>-name
                          text = <fs_param>-text
                          status = |{ status_icon_undef } { co_undef }|
                          t_color = me->set_color( abap_undefined ) ) TO it_ui_params.

      ENDCASE.

    ENDLOOP.

    DATA: it_fieldcat TYPE lvc_t_fcat.

    IF cell_type = co_celltype_button.
      it_fieldcat = VALUE lvc_t_fcat( ( fieldname = 'NAME'
                                        outputlen = 15
                                        coltext = 'Parameter'
                                        style = alv_style_font_bold + alv_style_color_int_group )
                                      ( fieldname = 'TEXT'
                                        outputlen = 20
                                        coltext = 'Description' )
                                      ( fieldname = co_status
                                        outputlen = 10
                                        coltext = 'Switch'
                                        style = alv_style_button
                                        icon = abap_true
                                        fix_column = abap_true ) ).
    ELSE.
      it_fieldcat = VALUE lvc_t_fcat( ( fieldname = 'NAME'
                                        outputlen = 15
                                        coltext = 'Parameter'
                                        style = alv_style_font_bold + alv_style_color_int_group )
                                      ( fieldname = 'TEXT'
                                        outputlen = 20
                                        coltext = 'Description' )
                                      ( fieldname = co_status
                                        outputlen = 10
                                        coltext = 'Switch'
                                        hotspot = abap_true
                                        icon = abap_true
                                        fix_column = abap_true ) ).
    ENDIF.

    DATA(s_layout) = VALUE lvc_s_layo( stylefname = 'T_STYLE'
                                       ctab_fname = 'T_COLOR'
                                       no_toolbar = abap_true
                                       no_headers = abap_false ).

    SET HANDLER on_hotspot_click FOR o_grid.
    SET HANDLER on_button_click FOR o_grid.

    o_grid->set_table_for_first_display( EXPORTING
                                           is_layout = s_layout
                                         CHANGING
                                           it_outtab = it_ui_params
                                           it_fieldcatalog = it_fieldcat ).

  ENDMETHOD.

* Parameterliste zurückholen
  METHOD get_params.
    rv_it_parameters = it_params.
  ENDMETHOD.

* Parameter hinzufügen
  METHOD add_parameter.
    APPEND VALUE #( name   = parameter-name
                    text   = parameter-text
                    status = parameter-status ) TO it_params.
  ENDMETHOD.

* Hintergrundfarben setzen
  METHOD set_color.

    CASE status.
      WHEN abap_true.
* Grün / ON
        color = VALUE #( ( fname = co_status color-col = co_color_on ) ).
      WHEN abap_false.
* Rot / OFF
        color = VALUE #( ( fname = co_status color-col = co_color_off ) ).
      WHEN abap_undefined.
* Gelb / UNDEF
        color = VALUE #( ( fname = co_status color-col = co_color_undef ) ).
    ENDCASE.

  ENDMETHOD.

  METHOD on_hotspot_click.

* geklickte ALV-Zeile holen
    READ TABLE it_ui_params ASSIGNING FIELD-SYMBOL(<ui_param>) INDEX e_row_id-index.
* korrespondierenden Parameter ermitteln
    READ TABLE it_params ASSIGNING FIELD-SYMBOL(<param>) WITH KEY name = <ui_param>-name.

    CASE <param>-status.
      WHEN abap_true.
* ON->OFF
        <ui_param>-status = |{ status_icon_off } { co_off }|.
        <ui_param>-t_color = me->set_color( abap_false ).
        <param->-status = abap_false.
      WHEN abap_false.
* OFF->UNDEF
        <ui_param>-status = |{ status_icon_undef } { co_undef }|.
        <ui_param>-t_color = me->set_color( abap_undefined ).
        <param>-status = abap_undefined.
      WHEN abap_undefined.
* UNDEF->ON
        <ui_param>-status = |{ status_icon_on } { co_on }|.
        <ui_param>-t_color = me->set_color( abap_true ).
        <param>-status = abap_true.
    ENDCASE.

    o_grid->refresh_table_display( is_stable = VALUE lvc_s_stbl( row = abap_true
                                                                 col = abap_true )
                                   i_soft_refresh = abap_false ).
  ENDMETHOD.

  METHOD on_button_click.
* geklickte ALV-Zeile holen
    READ TABLE it_ui_params ASSIGNING FIELD-SYMBOL(<ui_param>) INDEX es_row_no-row_id.
* korrespondierenden Parameter ermitteln
    READ TABLE it_params ASSIGNING FIELD-SYMBOL(<param>) WITH KEY name = <ui_param>-name.

    CASE <param>-status.
      WHEN abap_true.
* ON->OFF
        <ui_param>-status = status_icon_off.
        <ui_param>-t_color = me->set_color( abap_false ).
        <param>-status = abap_false.
      WHEN abap_false.
* OFF->UNDEF
        <ui_param>-status = status_icon_undef.
        <ui_param>-t_color = me->set_color( abap_undefined ).
        <param>-status = abap_undefined.
      WHEN abap_undefined.
* UNDEF->ON
        <ui_param>-status = status_icon_on.
        <ui_param>-t_color = me->set_color( abap_true ).
        <param>-status = abap_true.
    ENDCASE.

    o_grid->refresh_table_display( is_stable = VALUE lvc_s_stbl( row = abap_true
                                                                 col = abap_true )
                                   i_soft_refresh = abap_false ).
  ENDMETHOD.


ENDCLASS.

**********************************************************************
*
* Datentypen, Variablen, Konstanten
*
**********************************************************************
DATA: o_main TYPE REF TO lcl_main.

**********************************************************************
*
* SELECTION-SCREEN
*
**********************************************************************
SELECTION-SCREEN BEGIN OF SCREEN 2000.
SELECTION-SCREEN END OF SCREEN 2000.

**********************************************************************
*
* INITIALIZATION
*
**********************************************************************
INITIALIZATION.

  o_main = NEW #( o_parent = NEW cl_gui_docking_container( side = cl_gui_docking_container=>dock_at_right
                                                           extension = 400
                                                           no_autodef_progid_dynnr = abap_true ) ).

  o_main->add_parameter( VALUE #( name = 'SHOW_RESULT' text = 'Ergebnisse anzeigen' status = abap_true ) ).
  o_main->add_parameter( VALUE #( name = 'HIDE_COLS' text = 'Spalten ausblenden' status = abap_false ) ).
  o_main->add_parameter( VALUE #( name = 'DELETE_ON_EXIT' text = 'Beim Beenden löschen' status = abap_undefined ) ).

**********************************************************************
*
* AT SELECTION-SCREEN OUTPUT
*
**********************************************************************
AT SELECTION-SCREEN OUTPUT.

  IF o_main IS BOUND.
* Flag cell_type triggert den Typ (Hotspot / Button) des Klick-Feldes
    o_main->init_grid( cell_type = lcl_main=>co_celltype_hotspot ).
  ENDIF.

**********************************************************************
*
* AT SELECTION-SCREEN
*
**********************************************************************
AT SELECTION-SCREEN.
* wenn "Ausführen" (F8) geklickt wurde
  IF sy-ucomm = 'CRET'.
    cl_demo_output=>display_data( o_main->get_params( ) ).
  ENDIF.

**********************************************************************
*
* START-OF-SELECTION
*
**********************************************************************
START-OF-SELECTION.
* leeres Selektionbild 2000 anzeigen
  CALL SELECTION-SCREEN 2000.