[ABAP] SALV-Table – Gruppen für Spaltenvorrat im SALV-Layout

SELECT * FROM sflight INTO TABLE @DATA(it_sflight).

TRY.
* SALV-Table
    DATA: o_salv TYPE REF TO cl_salv_table.

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

* Grundeinstellungen
    o_salv->get_functions( )->set_all( abap_true ).
    o_salv->get_columns( )->set_optimize( abap_true ).
    o_salv->get_display_settings( )->set_list_header( 'Aggregationen' ).
    o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
    o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

* Spaltenüberschriften: technischer Name und Beschreibungstexte, Short Text und Medium Text leer lassen für Autosize
    LOOP AT o_salv->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<c>).
      DATA(o_col) = <c>-r_column.
      o_col->set_short_text( || ).
      o_col->set_medium_text( || ).
      o_col->set_long_text( |{ o_col->get_columnname( ) } [{ o_col->get_long_text( ) }]| ).
    ENDLOOP.

* Gruppen definieren --> verfügbar im Grid unter Toolbar-Button "Layout ändern ..." --> Spaltenvorrat
    o_salv->get_functional_settings( )->get_specific_groups( )->add_specific_group( id = 'GRP1' text = 'Spaltengruppe 1' ).
    o_salv->get_functional_settings( )->get_specific_groups( )->add_specific_group( id = 'GRP2' text = 'Spaltengruppe 2' ).

* Spalten GRP1 zuordnen
    DATA(o_col_max) = CAST cl_salv_column_list( o_salv->get_columns( )->get_column( 'SEATSMAX' ) ).
    o_col_max->set_specific_group( id = 'GRP1' ).
    DATA(o_col_occ) = CAST cl_salv_column_list( o_salv->get_columns( )->get_column( 'SEATSOCC' ) ).
    o_col_occ->set_specific_group( id = 'GRP1' ).

* Spalten GRP2 zuordnen
    DATA(o_col_max_b) = CAST cl_salv_column_list( o_salv->get_columns( )->get_column( 'SEATSMAX_B' ) ).
    o_col_max_b->set_specific_group( id = 'GRP2' ).
    DATA(o_col_occ_b) = CAST cl_salv_column_list( o_salv->get_columns( )->get_column( 'SEATSOCC_B' ) ).
    o_col_occ_b->set_specific_group( id = 'GRP2' ).
    DATA(o_col_max_f) = CAST cl_salv_column_list( o_salv->get_columns( )->get_column( 'SEATSMAX_F' ) ).
    o_col_max_f->set_specific_group( id = 'GRP2' ).
    DATA(o_col_occ_f) = CAST cl_salv_column_list( o_salv->get_columns( )->get_column( 'SEATSOCC_F' ) ).
    o_col_occ_f->set_specific_group( id = 'GRP2' ).

* Anzeige
    o_salv->display( ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Zwei SALV-Grids in einem Splittercontainer anzeigen

* Daten für SALV-Grid oben
SELECT *
  INTO TABLE @DATA(it_scarr)
  FROM scarr.

* Daten für SALV-Grid unten
SELECT *
  INTO TABLE @DATA(it_sflight)
  FROM sflight.

* Referenzen auf GUI-Objekte
* Splitter
DATA: o_splitter_main TYPE REF TO cl_gui_splitter_container.
* Splitter-Container oben
DATA: o_container_o   TYPE REF TO cl_gui_container.
* Splitter-Container unten
DATA: o_container_u   TYPE REF TO cl_gui_container.

* Splitter auf default_screen erzeugen
o_splitter_main = NEW #( parent                  = cl_gui_container=>default_screen
                         no_autodef_progid_dynnr = abap_true       " wichtig
                         rows                    = 2
                         columns                 = 1 ).

* Höhe oberer Splitter in %
o_splitter_main->set_row_height( id = 1 height = 40 ).

* REF auf oberen und unteren Splitcontainer holen
o_container_o = o_splitter_main->get_container( row = 1 column = 1 ).
o_container_u = o_splitter_main->get_container( row = 2 column = 1 ).

* SALV-Table oben mit Fluggesellschaften
DATA: o_salv_o TYPE REF TO cl_salv_table.

cl_salv_table=>factory( EXPORTING
                          r_container  = o_container_o
                        IMPORTING
                          r_salv_table = o_salv_o
                        CHANGING
                          t_table      = it_scarr ).

* Grundeinstellungen
o_salv_o->get_functions( )->set_all( abap_true ).
o_salv_o->get_columns( )->set_optimize( abap_true ).
o_salv_o->get_display_settings( )->set_list_header( 'Fluggesellschaften' ).
o_salv_o->get_display_settings( )->set_striped_pattern( abap_true ).
o_salv_o->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

* Spaltenüberschriften: technischer Name und Beschreibungstexte
LOOP AT o_salv_o->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<so>).
  DATA(o_col_o) = <so>-r_column.
  o_col_o->set_short_text( || ).
  o_col_o->set_medium_text( || ).
  o_col_o->set_long_text( |{ o_col_o->get_columnname( ) }| ).
ENDLOOP.

* SALV-Grid anzeigen
o_salv_o->display( ).

* SALV-Table unten mit Flügen
DATA: o_salv_u TYPE REF TO cl_salv_table.

cl_salv_table=>factory( EXPORTING
                          r_container  = o_container_u
                        IMPORTING
                          r_salv_table = o_salv_u
                        CHANGING
                          t_table      = it_sflight ).

* Grundeinstellungen
o_salv_u->get_functions( )->set_all( abap_true ).
o_salv_u->get_columns( )->set_optimize( abap_true ).
o_salv_u->get_display_settings( )->set_list_header( 'Flüge' ).
o_salv_u->get_display_settings( )->set_striped_pattern( abap_true ).
o_salv_u->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

* Spaltenüberschriften: technischer Name und Beschreibungstexte
LOOP AT o_salv_u->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<su>).
  DATA(o_col_u) = <su>-r_column.
  o_col_u->set_short_text( || ).
  o_col_u->set_medium_text( || ).
  o_col_u->set_long_text( |{ o_col_u->get_columnname( ) }| ).
ENDLOOP.

* SALV-Grid anzeigen
o_salv_u->display( ).

* leere Toolbar ausblenden
cl_abap_list_layout=>suppress_toolbar( ).

* Erzwingen von cl_gui_container=>default_screen
WRITE: space.

[ABAP] SALV-Table als F4-Suchhilfe-PopUp verwenden

CLASS lcl_salv_f4 DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: get_selected_value
      IMPORTING
                i_table_name      TYPE string
                i_display_columns TYPE string DEFAULT '*'
                i_sel_column      TYPE string
                i_max_datacnt     TYPE i OPTIONAL
                i_top             TYPE i DEFAULT 3
                i_bottom          TYPE i DEFAULT 25
                i_left            TYPE i DEFAULT 25
                i_right           TYPE i DEFAULT 160
      RETURNING VALUE(ret_val)    TYPE string.
ENDCLASS.

CLASS lcl_salv_f4 IMPLEMENTATION.
  METHOD get_selected_value.

    ret_val = ''.

    TRY.
        IF i_display_columns NE ''.
* Datenreferenzen auf Table und Row
          DATA: o_table TYPE REF TO data.
          DATA: o_row TYPE REF TO data.

          FIELD-SYMBOLS: <table> TYPE STANDARD TABLE.
          FIELD-SYMBOLS: <row> TYPE any.

          CREATE DATA o_table TYPE STANDARD TABLE OF (i_table_name).
          ASSIGN o_table->* TO <table>.

          CREATE DATA o_row LIKE LINE OF <table>.
          ASSIGN o_row->* TO <row>.

* SELECT-Statement anpassen
          IF i_max_datacnt IS NOT INITIAL.
            SELECT (i_display_columns) FROM (i_table_name) INTO CORRESPONDING FIELDS OF TABLE @<table> UP TO @i_max_datacnt ROWS.
          ELSE.
            SELECT (i_display_columns) FROM (i_table_name) INTO CORRESPONDING FIELDS OF TABLE @<table>.
          ENDIF.

* SALV-Grid erzeugen
          DATA: o_salv TYPE REF TO cl_salv_table.

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

          o_salv->get_functions( )->set_default( abap_true ).
          o_salv->get_columns( )->set_optimize( abap_true ).
          o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
          o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>single ).
          o_salv->get_display_settings( )->set_list_header( 'Auswahl' ).

* Strukturbeschreibung einer Tabellenzeile
          DATA: o_struct TYPE REF TO cl_abap_structdescr.
          o_struct ?= cl_abap_typedescr=>describe_by_data( <row> ).

* Komponenten der Struktur
*          DATA: it_comp_tab TYPE abap_component_tab.
          DATA(it_comp_tab) = o_struct->get_components( ).

          DATA(it_columns) = o_salv->get_columns( ).
          DATA: lv_col TYPE REF TO cl_salv_column.

          LOOP AT it_comp_tab INTO DATA(lv_comp).
* Spalte anhand des Komponentennamens holen
            lv_col ?= it_columns->get_column( |{ lv_comp-name }| ).

* wenn Anzeigespalte als Komponente der Struktur verfügbar, dann Spalte anzeigen
            IF ( i_display_columns CS lv_comp-name ) OR ( i_display_columns = '*' ).
              lv_col->set_visible( abap_true ).
            ELSE.
              lv_col->set_visible( abap_false ).
            ENDIF.
          ENDLOOP.

* Popup-SALV-Grid anzeigen
          o_salv->set_screen_popup( start_column = i_left
                                    end_column   = i_right
                                    start_line   = i_top
                                    end_line     = i_bottom ).

* Popup anzeigen und auf Klick warten
          o_salv->display( ).

* Auswahl auswerten
          DATA(it_sel_rows) = o_salv->get_selections( )->get_selected_rows( ).

* Nummer der ersten gewählten Tabellenzeile holen -> it_sel_rows[ 1 ]
* Tabellenzeile ermitteln -> <table>[ ... ]
* und Feldwert anhand der Spaltennummer auslesen -> COMPONENT i_sel_column
          ASSIGN COMPONENT i_sel_column OF STRUCTURE <table>[ it_sel_rows[ 1 ] ] TO FIELD-SYMBOL(<cell>).

* Rückgabewert
          ret_val = <cell>.

        ENDIF.

      CATCH cx_root.

    ENDTRY.

  ENDMETHOD.
ENDCLASS.

PARAMETERS: p_par TYPE string OBLIGATORY.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_par.
  p_par = lcl_salv_f4=>get_selected_value( i_table_name = 'spfli'
                                           i_display_columns = 'mandt, carrid, connid, cityto, airpfrom'
                                           i_sel_column = 'cityto' ).