[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' ).