ALV tutorial 08 – coloring cells

Example of coloring single cells in ALV grid

REPORT  z_alv_demo_08.
TYPES: BEGIN OF ty_data.
        INCLUDE STRUCTURE sflight.
TYPES:  cell_table TYPE lvc_t_scol,             " 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,
    <fs_cellcolor> type lvc_s_scol.

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

  LOOP AT gt_data ASSIGNING <fs_data>.
* Color whole row in case no seats are booked
    IF <fs_data>-seatsocc = 0 AND
       <fs_data>-seatsocc_b = 0 AND
       <fs_data>-seatsocc_f = 0.
      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-nokeycol = 'X'.
    endif.

* Color only columns where the travel class is full
    IF <fs_data>-seatsocc = <fs_data>-seatsmax.
      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-fname = 'SEATSOCC'.
      <fs_cellcolor>-nokeycol = 'X'.

      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-fname = 'SEATSMAX'.
      <fs_cellcolor>-nokeycol = 'X'.
    endif.

    if <fs_data>-seatsocc_b = <fs_data>-seatsmax_b.
      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-fname = 'SEATSOCC_B'.
      <fs_cellcolor>-nokeycol = 'X'.

      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-fname = 'SEATSMAX_B'.
      <fs_cellcolor>-nokeycol = 'X'.
    endif.

    if <fs_data>-seatsocc_f = <fs_data>-seatsmax_f.
      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-fname = 'SEATSOCC_F'.
      <fs_cellcolor>-nokeycol = 'X'.

      APPEND INITIAL LINE TO <fs_data>-cell_table ASSIGNING <fs_cellcolor>.
      <fs_cellcolor>-color-col = 6.
      <fs_cellcolor>-color-int = 1.
      <fs_cellcolor>-fname = 'SEATSMAX_F'.
      <fs_cellcolor>-nokeycol = 'X'.
    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.
  gs_layout-ctab_fname = 'CELL_TABLE'.
  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

2 thoughts on “ALV tutorial 08 – coloring cells

Leave a Reply