[ABAP] SALV IDA: Icons und Buttons einfügen, Klick-Events abfangen

CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    INTERFACES: if_salv_ida_calc_field_handler.

    METHODS: on_cell_action FOR EVENT cell_action OF if_salv_gui_field_display_opt
      IMPORTING
          ev_field_name
          eo_row_data
          sender.

  PRIVATE SECTION.
    TYPES:
      BEGIN OF ty_calc_field,
        icon   TYPE char4,
        button TYPE lvc_value,
      END OF ty_calc_field.

    TYPES: BEGIN OF ty_line.
            INCLUDE TYPE sflight.
            INCLUDE TYPE ty_calc_field.
    TYPES: END OF ty_line.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.

  METHOD on_cell_action.
    DATA: lv_row TYPE ty_line.

    eo_row_data->get_row_data( EXPORTING
                                 iv_request_type = if_salv_gui_selection_ida=>cs_request_type-all_fields
                               IMPORTING
                                 es_row = lv_row ).

    cl_salv_ida_show_data_row=>display( iv_text = |Datenfeld: { ev_field_name }|
                                        is_data = lv_row ).
  ENDMETHOD.

  METHOD if_salv_ida_calc_field_handler~get_calc_field_structure.
* Struktur-Deklaration für zus. Felder ICON und BUTTON zurückgeben
    ro_calc_field_structure = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_name( 'TY_CALC_FIELD' ) ).
  ENDMETHOD.

  METHOD if_salv_ida_calc_field_handler~get_requested_fields.
  ENDMETHOD.

  METHOD if_salv_ida_calc_field_handler~calculate_line.

* Daten akt. Zeile holen
    DATA(lv_sflight) = CONV sflight( is_data_base_line ).

* Freie Sitze ausrechnen
    DATA(lv_cnt_free_seats) = lv_sflight-seatsmax - lv_sflight-seatsocc.

* Calculated Fields bestimmen
    es_calculated_fields = COND ty_calc_field( WHEN lv_cnt_free_seats = 0  THEN VALUE #( icon = icon_red_light    button = icon_delete )
                                               WHEN lv_cnt_free_seats > 10 THEN VALUE #( icon = icon_green_light  button = icon_okay )
                                                                           ELSE VALUE #( icon = icon_yellow_light button = space )
                                             ).

  ENDMETHOD.

  METHOD if_salv_ida_calc_field_handler~start_page.
  ENDMETHOD.

  METHOD if_salv_ida_calc_field_handler~end_page.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  DATA(o_handler) = NEW lcl_event_handler( ).

  DATA(o_alv) = cl_salv_gui_table_ida=>create( iv_table_name         = 'SFLIGHT'
                                               io_calc_field_handler = o_handler ). " Referenz für Fieldhandler

* Single-Selection
  o_alv->selection( )->set_selection_mode( if_salv_gui_selection_ida=>cs_selection_mode-single ).

* Tooltips für Icons
  DATA(it_icon_tooltip) = VALUE if_salv_gui_types_ida=>yts_icon_tooltip( ( icon    = icon_yellow_light
                                                                           tooltip = 'Weniger als 10 Sitze.' )
                                                                         ( icon    = icon_green_light
                                                                           tooltip = 'Ausreichend Sitze.' )
                                                                         ( icon    = icon_red_light
                                                                           tooltip = 'Keine Sitze mehr frei.' ) ).

* Tooltips setzen: Feldname 'ICON' muss identisch mit ty_calc_field-icon sein!
  o_alv->field_catalog( )->display_options( )->set_tooltip_for_icons( iv_field_name = 'ICON'
                                                                      its_tooltips  = it_icon_tooltip ).

* Überschrift für Spalte 'ICON': Feldname 'ICON' muss identisch mit ty_calc_field-icon sein!
  o_alv->field_catalog( )->set_field_header_texts( iv_field_name  = 'ICON'
                                                   iv_header_text = 'Status' ).

* Zellen der Spalte 'ICON' sind klickbar: Feldname 'ICON' muss identisch mit ty_calc_field-icon sein!
  o_alv->field_catalog( )->display_options( )->display_as_link_to_action( iv_field_name = 'ICON' ).


* Tooltips für Buttons: Feldname 'BUTTON' muss identisch mit ty_calc_field-icon sein!
  o_alv->field_catalog( )->display_options( )->set_tooltip_for_icons( iv_field_name           = 'BUTTON'
                                                                      iv_use_standard_tooltip = abap_true ).

* Wenn leere Buttonwerte in der Funktion if_salv_ida_calc_field_handler~calculate_line erzeugt werden (INITIAL),
* dann keine Buttons anzeigen
* Feldname 'BUTTON' muss identisch mit ty_calc_field-button sein!
  o_alv->field_catalog( )->display_options( )->display_as_button( iv_field_name               = 'BUTTON'
                                                                  iv_hide_if_value_is_initial = abap_true ).

* Überschrift für Spalte 'BUTTON': Feldname 'BUTTON' muss identisch mit ty_calc_field-button sein!
  o_alv->field_catalog( )->set_field_header_texts( iv_field_name  = 'BUTTON'
                                                   iv_header_text = 'Aktion' ).

* Click-Handler für Icons und Buttons aktivieren
  SET HANDLER o_handler->on_cell_action FOR o_alv->field_catalog( )->display_options( ).

* Tabelle anzeigen
  o_alv->fullscreen( )->display( ).

[ABAP] SALV IDA: SAP List Viewer with Integrated Data Access

* Beispielprogramme:
* Paket: SALV_IDA_TEST

CONSTANTS: co_table_name TYPE dbtabl VALUE 'SBOOK'.

DATA: lv_carrid TYPE sbook-carrid.

SELECT-OPTIONS: so_carr FOR lv_carrid.

START-OF-SELECTION.

  TRY.
* SALV Table IDA erzeugen
      DATA(o_salv_ida) = cl_salv_gui_table_ida=>create( iv_table_name = co_table_name ).

* Authority Check (löst evtl. Exception aus, wenn Berechtigungen fehlen)
      o_salv_ida->add_authorization_for_object( iv_authorization_object = 'S_CARRID'
                                                it_activities           = VALUE #( ( auth_field = 'ACTVT' value = '03' ) )
                                                it_field_mapping        = VALUE #( ( auth_field = 'CARRID' view_field = 'CARRID' ) ) ).

* SELECT-Konditionen festlegen
      DATA(o_cond_factory) = o_salv_ida->condition_factory( ).

* RANGE definieren: SELECT-OPTIONS
      DATA(o_ranges) = NEW cl_salv_range_tab_collector( ).
      o_ranges->add_ranges_for_name( iv_name = 'CARRID' it_ranges = so_carr[] ).
      o_ranges->get_collected_ranges( IMPORTING
                                        et_named_ranges = DATA(it_name_range_pairs) ).
* RANGE definieren: manuell
      DATA(it_fixed_ranges) = VALUE if_salv_service_types=>yt_named_ranges( ( name = 'FORCURKEY' sign = 'E' option = 'EQ' low = 'GBP' high = '' ) ).

      APPEND LINES OF it_fixed_ranges TO it_name_range_pairs.

* SELECT-Konditionen und RANGES übergeben
      o_salv_ida->set_select_options( it_ranges    = it_name_range_pairs
                                      io_condition = o_cond_factory->equals( name = 'FLDATE' value = '20120523' )->and(
                                                     o_cond_factory->covers_pattern( name = 'PASSNAME' pattern = '*Anna*')->and(
                                                     o_cond_factory->equals( name = 'FORCURKEY' value = 'EUR' )->or(
                                                     o_cond_factory->equals( name = 'FORCURKEY' value = 'USD' ) ) ) ) ).

* Spaltenüberschrift ändern
      o_salv_ida->field_catalog( )->set_field_header_texts( iv_field_name        = 'PASSNAME'
                                                            iv_header_text       = 'Name des Passagiers'
                                                            iv_tooltip_text      = 'Standard: 40 Zeichen zur Verfügung'
                                                            iv_tooltip_text_long = 'Erweiterung: 100 Zeichen zur Verfügung bei richtiger SAP GUI und SAP Basis Version' ).

* Sortierungen festlegen
      DATA(it_sort_order) = VALUE if_salv_gui_types_ida=>yt_sort_rule( ( field_name = 'PASSNAME'
                                                                         descending = abap_false
                                                                         is_grouped = abap_false ) ).

      o_salv_ida->default_layout( )->set_sort_order( it_sort_order = it_sort_order ).

* manuelle Sortierung für Spalte ausschalten
      o_salv_ida->field_catalog( )->disable_sort( iv_field_name = 'PASSNAME' ).

* Filter ausschalten
      o_salv_ida->field_catalog( )->disable_filter( iv_field_name = 'PASSNAME' ).

* nur bestimmte Spalten darstellen 1
      o_salv_ida->field_catalog( )->get_all_fields( IMPORTING ets_field_names = DATA(it_field_names) ).
      DELETE it_field_names WHERE table_line CP 'SEATS*'.

* nur bestimmte Spalten darstellen 2
*        DATA(it_field_names) = VALUE if_salv_gui_types_ida=>yts_field_name( ( CONV #( 'CARRID' ) )
*                                                                            ( CONV #( 'CONNID' ) )
*                                                                            ( CONV #( 'FLDATE' ) )
*                                                                            ( CONV #( 'FORCURKEY' ) )
*                                                                            ( CONV #( 'CUSTTYPE' ) )
*                                                                            ( CONV #( 'PASSNAME' ) ) ).

      o_salv_ida->field_catalog( )->set_available_fields( its_field_names = it_field_names ).

* passenden Beschreibungstext anhand der Domäne darstellen
      o_salv_ida->field_catalog( )->display_options( )->set_formatting( iv_field_name        = 'CUSTTYPE'
                                                                        iv_presentation_mode = if_salv_gui_types_ida=>cs_presentation_mode-description ).

* Textsuche im Gitter für Spalten 'CARRID' und 'PASSNAME' aktivieren (Button mit Lupe oben rechts)
      o_salv_ida->field_catalog( )->enable_text_search( 'CARRID' ).
      o_salv_ida->field_catalog( )->enable_text_search( 'PASSNAME' ).
      o_salv_ida->standard_functions( )->set_text_search_active( abap_true ).

* Layouts verwalten
      o_salv_ida->layout_persistence( )->set_persistence_options( is_persistence_key           = VALUE #( report_name = sy-repid )
                                                                  i_global_save_allowed        = abap_true
                                                                  i_user_specific_save_allowed = abap_true ).

      o_salv_ida->toolbar( )->enable_listbox_for_layouts( ).

* Titel
      o_salv_ida->display_options( )->set_title( 'Überschrift' ).

* Text für leere Tabelle
      o_salv_ida->display_options( )->set_empty_table_text( 'Keine Daten vorhanden.' ).

* Zebrastreifen
      o_salv_ida->display_options( )->enable_alternating_row_pattern( ).

* Datenbankfähigkeiten abfragen
      IF abap_true = cl_salv_gui_table_ida=>db_capabilities( )->is_text_search_supported( ).
* unscharfe Suche (Fuzzy) -> wird nur von HANA unterstützt
        o_salv_ida->text_search( )->set_field_similarity( '0.8' ).
        o_salv_ida->text_search( )->set_search_term( |Fehler| ).
      ENDIF.

* Wird eine Beschränkung der Anzahl der Datensätze empfohlen?
      IF cl_salv_gui_table_ida=>db_capabilities( )->is_max_rows_recommended( ).
* max. Anzahl der Datensätze beschränken
        o_salv_ida->set_maximum_number_of_rows( iv_number_of_rows = 500 ).
      ENDIF.

      o_salv_ida->fullscreen( )->display( ).

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

Links