[ABAP] SALV-Table – Zeilen/Zellen farbig anzeigen

Variante 1 (Zeilen farbig anzeigen)

TYPES: BEGIN OF ty_s_sflight,
         fldate    TYPE s_date,
         planetype TYPE s_planetye,
         seatsmax  TYPE s_seatsmax,
         seatsocc  TYPE s_seatsocc,
         color     TYPE lvc_t_scol, " Farbspalte
       END OF ty_s_sflight.

TYPES: ty_it_sflight TYPE STANDARD TABLE OF ty_s_sflight WITH DEFAULT KEY.

START-OF-SELECTION.

  TRY.
      DATA(it_sflight) = VALUE ty_it_sflight( ).

      SELECT FROM sflight
        FIELDS
             fldate,
             planetype,
             seatsmax,
             seatsocc
        INTO CORRESPONDING FIELDS OF TABLE @it_sflight.

      DATA: o_alv TYPE REF TO cl_salv_table.

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

* Farben in der Tabellen-Spalte 'COLOR' setzen
      LOOP AT it_sflight ASSIGNING FIELD-SYMBOL(<f>).
* Wenn >= 370 belegte Plätze in der Economy Class
        IF <f>-seatsocc >= 370.
* dann in der Spalte COLOR für jede Tabellen-Zeile (Row) eine Farbtabelle einfügen
          <f>-color = VALUE #( ( color-col = col_positive  " grün
                                 color-int = 0
                                 color-inv = 0
                               )
                             ).
        ENDIF.
      ENDLOOP.

* Namen der Farbspalte setzen
      o_alv->get_columns( )->set_color_column( 'COLOR' ).

* Grid anzeigen
      o_alv->display( ).
    CATCH cx_root INTO DATA(e_text).
      WRITE: / e_text->get_text( ).
  ENDTRY.

Variante 2 (einzelne Zellen farbig anzeigen)

TYPES: BEGIN OF ty_s_sflight,
         fldate    TYPE s_date,
         planetype TYPE s_planetye,
         seatsmax  TYPE s_seatsmax,
         seatsocc  TYPE s_seatsocc,
         color     TYPE lvc_t_scol, " Farbspalte
       END OF ty_s_sflight.

TYPES: ty_it_sflight TYPE STANDARD TABLE OF ty_s_sflight WITH DEFAULT KEY.

START-OF-SELECTION.

  TRY.
      DATA(it_sflight) = VALUE ty_it_sflight( ).

      SELECT FROM sflight
        FIELDS
             fldate,
             planetype,
             seatsmax,
             seatsocc
        INTO CORRESPONDING FIELDS OF TABLE @it_sflight.

      DATA: o_alv TYPE REF TO cl_salv_table.

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

* Farben in der Tabellen-Spalte 'COLOR' setzen
      LOOP AT it_sflight ASSIGNING FIELD-SYMBOL(<f>).
* Wenn >= 370 belegte Plätze in der Economy Class
        IF <f>-seatsocc >= 370.
* dann in der Spalte COLOR für jede einzelne Zelle (Cell) der Spalte SEATSOCC eine Farbtabelle einfügen
          <f>-color = VALUE #( ( fname     = 'SEATSOCC'
                                 color-col = col_positive  " grün
                                 color-int = 0
                                 color-inv = 0
                               )
                             ).
        ENDIF.
      ENDLOOP.

* Namen der Farbspalte setzen
      o_alv->get_columns( )->set_color_column( 'COLOR' ).

* Grid anzeigen
      o_alv->display( ).
    CATCH cx_root INTO DATA(e_text).
      WRITE: / e_text->get_text( ).
  ENDTRY.

Links