ALV tutorial 07 – coloring rows

Example of coloring whole rows in ALV

REPORT  z_alv_demo_07.
TYPES: BEGIN OF ty_data.
        INCLUDE STRUCTURE sflight.
TYPES:  row_color(4) TYPE c,             " field to hold the row color
       END OF ty_data.

DATA: gt_data TYPE TABLE OF ty_data,
      g_grid TYPE REF TO cl_gui_alv_grid,
      gs_layout TYPE lvc_s_layo,
      gt_fieldcat TYPE lvc_t_fcat.

CONSTANTS:
  c_max_rows TYPE i VALUE 100.

CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Form  read_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM read_data.
  FIELD-SYMBOLS:
    <fs_data> TYPE ty_data.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
    FROM sflight
    UP TO c_max_rows ROWS.

  LOOP AT gt_data ASSIGNING <fs_data>.
    IF <fs_data>-seatsocc = <fs_data>-seatsmax OR
       <fs_data>-seatsocc_b = <fs_data>-seatsmax_b OR
      <fs_data>-seatsocc_f = <fs_data>-seatsmax_f.
      " Some class is full
* Colour code :                                    *
* Colour is a 4-char field where :                 *
* - 1st char = C (color property)                  *
* - 2nd char = color code (from 0 to 7)            *
*                     0 = background color         *
*                     1 = blue                     *
*                     2 = gray                     *
*                     3 = yellow                   *
*                     4 = blue/gray                *
*                     5 = green                    *
*                     6 = red                      *
*                     7 = orange                   *
* - 3rd char = intensified (0=off, 1=on)           *
* - 4th char = inverse display (0=off, 1=on)       *
* i.e. 'C310'

      <fs_data>-row_color = 'C310'.             " Yellow - some travel class is full
    ENDIF.
  ENDLOOP.
ENDFORM.                    "read_data

*&---------------------------------------------------------------------*
*&      Form  display_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_grid.
  PERFORM get_layout.
*  PERFORM get_fieldcat.

  CREATE OBJECT g_grid
    EXPORTING
      i_parent = cl_gui_container=>default_screen.

  CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      i_structure_name              = 'SFLIGHT'
      is_layout                     = gs_layout
    CHANGING
      it_outtab                     = gt_data
*      it_fieldcatalog               = gt_fieldcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    "display_grid
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'MAIN'.
  SET TITLEBAR 'ALV_EXAMPLES'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  DISPLAY_GRID  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE display_grid OUTPUT.
  PERFORM read_data.
  PERFORM display_grid.
ENDMODULE.                 " DISPLAY_GRID  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
*   to react on oi_custom_events:
  CALL METHOD cl_gui_cfw=>dispatch.
  CASE sy-ucomm.
    WHEN 'BACK' OR
         'EXIT' OR
         'CANCEL'.
      LEAVE PROGRAM.
    WHEN OTHERS.
*     do nothing
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  GET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*    p1        text
*   TYPE lvc_s_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'SFLIGHT'
    CHANGING
      ct_fieldcat            = gt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  CHECK sy-subrc = 0.
ENDFORM.                    " GET_FIELDCAT

Leave a Reply