Search This Blog

Showing posts with label alv with traffic light. Show all posts
Showing posts with label alv with traffic light. Show all posts

Thursday, February 11, 2021

Display Traffic Lights in SAP ALV

 

Steps for Traffic Light in ALV Report.

1.     Add a column for traffic light.

 

            TYPES: BEGIN OF ty_mara.

            INCLUDE STRUCTURE mara.

            TYPES: icon TYPE c,  " Add field to hold traffic light value

              END OF ty_mara.

 

2.     Fetch data using select query and build a field catalog.


3.     Assign traffic light as per your requirement

    IF wa_mara-ersda  <  '01.01.2020'.
      wa_mara
-icon 1.  " Red Traffic Light.

Endif

4.     To display traffic lights in the ALV, fill the lights field name in the LIGHTS_FIELDNAME of ALV layout.


5.     Pass field catalog to function module ‘REUSE_ALV_GRID_DISPLAY’.











SAMPLE CODE : 



REPORT ZTRAFFIC_LIGHT.
TYPE-POOLSslis.  " SLIS contains all the ALV data types
*&---------------------------------------------------------------------*
*& Data Types
*&---------------------------------------------------------------------*
TYPESBEGIN OF ty_mara.
    INCLUDE STRUCTURE mara.
TYPESicon TYPE c,  " Add field to hold traffic light value
       END OF ty_mara.
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATAit_mara      TYPE TABLE OF ty_mara.
DATAwa_mara      TYPE ty_mara.
DATAit_fieldcat  TYPE slis_t_fieldcat_alv,
      wa_fieldcat  TYPE slis_fieldcat_alv.
DATAis_layout    TYPE slis_layout_alv.
DATAg_repid      TYPE sy-repid.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  g_repid sy-repid.
*Fetch data from the database
  SELECT UP TO 100 ROWS FROM mara INTO TABLE it_mara.

*Assign different traffic lights to each row based on condition
  LOOP AT it_mara INTO wa_mara.
    IF wa_mara-ersda  <  '01.01.2020'.
      wa_mara-icon 1.  " Red Traffic Light

    ELSEIF wa_mara-ersda =  '01.01.2020'.
      wa_mara-icon 2.  " Yellow Traffic Light

    ELSE.
      wa_mara-icon  3.  " Green Traffic Light
    ENDIF.

    MODIFY it_mara FROM wa_mara TRANSPORTING icon.
    CLEARwa_mara.
  ENDLOOP.

*Build field catalog
  wa_fieldcat-fieldname  'MATNR'.    " Fieldname in the data table
  wa_fieldcat-seltext_m  'MATNR'.   " Column description in the output
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.

  wa_fieldcat-fieldname  'ERSDA'.
  wa_fieldcat-seltext_m  'Created On'.
  APPEND wa_fieldcat TO it_fieldcat.
  CLEAR wa_fieldcat.



*Fill layout info.
*Fill traffic lights field name in the ALV layout
  is_layout-lights_fieldname 'ICON'.

*Pass data and field catalog to ALV function module to display ALV list
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program g_repid
      is_layout          is_layout
      it_fieldcat        it_fieldcat
    TABLES
      t_outtab           it_mara
    EXCEPTIONS
      program_error      1
      OTHERS             2.




Pages