[ABAP] SALV-Table: Icons und Tooltips anzeigen

TYPES: BEGIN OF ty_it_sflight,
         status    TYPE char10,            " ICON-Spalte
         fldate    TYPE sflight-fldate,
         planetype TYPE sflight-planetype,
         seatsmax  TYPE sflight-seatsmax,
         seatsocc  TYPE sflight-seatsocc,
         seatsdiff TYPE i,                 " Spalte für ausgerechnete Differenz der Sitze
       END OF ty_it_sflight.

DATA: it_sflight TYPE STANDARD TABLE OF ty_it_sflight.

START-OF-SELECTION.

* Daten holen
  SELECT fldate, planetype, seatsmax, seatsocc
    FROM sflight
    INTO CORRESPONDING FIELDS OF TABLE @it_sflight
    WHERE carrid = 'LH'.

* Daten aufbereiten
  LOOP AT it_sflight ASSIGNING FIELD-SYMBOL(<fs_line>).

* Differenz der Sitze ausrechnen und in Spalte SEATSDIFF speichern
    <fs_line>-seatsdiff = <fs_line>-seatsmax - <fs_line>-seatsocc.

* in Spalte STATUS (ICON-Spalte) entspr. der Differenz ICONS setzen
    IF <fs_line>-seatsdiff < 10.
      <fs_line>-status = icon_red_light.
    ELSEIF <fs_line->-seatsdiff < 20.
      <fs_line>-status = icon_yellow_light.
    ELSE.
      <fs_line>-status = icon_green_light.
    ENDIF.

  ENDLOOP.

  TRY.
      DATA: o_salv TYPE REF TO cl_salv_table.

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

      o_salv->get_functions( )->set_all( abap_true ).

      DATA: o_column TYPE REF TO cl_salv_column_table.
      DATA(o_columns) = o_salv->get_columns( ).

* Spalte STATUS als ICON-Spalte setzen
      o_column ?= o_columns->get_column( 'STATUS' ).
      o_column->set_icon( if_salv_c_bool_sap=>true ).
      o_column->set_alignment( if_salv_c_alignment=>centered ).
      o_column->set_long_text( 'Sitz-Status' ).

* Spalte SEATSDIFF formatieren
      o_column ?= o_columns->get_column( 'SEATSDIFF' ).
      o_column->set_alignment( if_salv_c_alignment=>right ).
      o_column->set_long_text( 'Freie Sitze' ).

* wertabhängige Tooltips setzen
      DATA(o_tooltips) = o_salv->get_functional_settings( )->get_tooltips( ).

      o_tooltips->add_tooltip( type = cl_salv_tooltip=>c_type_icon
                               value = |{ icon_green_light }|
                               tooltip = 'Ausreichend Sitze' ).

      o_tooltips->add_tooltip( type = cl_salv_tooltip=>c_type_icon
                               value = |{ icon_yellow_light }|
                               tooltip = 'Nur noch wenige Sitze' ).

      o_tooltips->add_tooltip( type = cl_salv_tooltip=>c_type_icon
                               value = |{ icon_red_light }|
                               tooltip = 'Nahezu voll' ).

      o_salv->display( ).

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