Introduction:
This document clearly explains how to add multiple traffic light icons in alv display and assign tool tip to this icons.
For Example ,I have taken table ZTRAFFICLIGHTS.
In order to display traffic lights in ALV display you have to include Type Pools:ICON and declare a variable suppose 'x' of type CHAR4.
And assign fieldcatalog-ICON = 'X' for the icon column while creating field catalog for ALV. You can define more than one variable of type CHAR4 to display icons.
Below is the source code:
*&---------------------------------------------------------------------*
*& Report ZTEST_TRAFFICLIGHTS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ztest_trafficlights.
TYPE-POOLS :slis,icon.
TABLES ztrafficlights.
*Types declaration
TYPES: BEGIN OF ty_trafficlights,
carrid TYPE ztrafficlights-carrid,
fdate TYPE ztrafficlights-fdate,
fprice TYPE ztrafficlights-fprice,
currency TYPE ztrafficlights-currency,
zseats TYPE ztrafficlights-zseats,
zseatsocc TYPE ztrafficlights-zseatsocc,
END OF ty_trafficlights.
TYPES:BEGIN OF ty_final,
g_icon TYPE char4,
g_icon1(30) TYPE c. "Second Icon field
INCLUDE TYPE ty_trafficlights.
TYPES: END OF ty_final.
*Global Data declaration
DATA :it_trafficlights TYPE TABLE OF ty_trafficlights,
it_final TYPE TABLE OF ty_final,
it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv.
* Selection screen
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS s_carrid FOR ztrafficlights-carrid.
SELECTION-SCREEN END OF BLOCK b1.
START-OF-SELECTION.
* Fetch records from Database
PERFORM fetch_records.
*Fill fieldcatalog
PERFORM fill_fieldcatalog.
END-OF-SELECTION.
*Display ALV
PERFORM display_alv.
*&---------------------------------------------------------------------*
*& Form FETCH_RECORDS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fetch_records .
*local data declarations
DATA :wa_final TYPE ty_final,
wa_trafficlights TYPE ty_trafficlights,
lv_temp TYPE p DECIMALS 2.
SELECT * FROM ztrafficlights INTO CORRESPONDING FIELDS OF TABLE it_trafficlights
WHERE carrid IN s_carrid.
LOOP AT it_trafficlights INTO wa_trafficlights.
CLEAR lv_temp.
lv_temp = ( wa_trafficlights-zseatsocc / wa_trafficlights-zseats ) .
IF lv_temp >= '0.9'.
* 90 % tickets are booked
wa_final-g_icon = icon_red_light.
CONCATENATE '@0A\Q' 'Almost Full' '@' INTO wa_final-g_icon1.
ELSEIF lv_temp >= '0.2' AND lv_temp < '0.9'.
* Less than 90% and more than 20% are booked
wa_final-g_icon = icon_yellow_light.
CONCATENATE '@09\Q' 'Filling Fast' '@' INTO wa_final-g_icon1.
ELSEIF lv_temp < '0.2'.
* Less than 20% tickets booked
wa_final-g_icon = icon_green_light.
CONCATENATE '@08\Q' 'Available' '@' INTO wa_final-g_icon1."
ENDIF.
wa_final-carrid = wa_trafficlights-carrid.
wa_final-fdate = wa_trafficlights-fdate.
wa_final-fprice = wa_trafficlights-fprice.
wa_final-currency = wa_trafficlights-currency.
wa_final-zseats = wa_trafficlights-zseats.
wa_final-zseatsocc = wa_trafficlights-zseatsocc.
APPEND wa_final TO it_final.
CLEAR wa_final.
ENDLOOP.
ENDFORM. " FETCH_RECORDS
*&---------------------------------------------------------------------*
*& Form FILL_FIELDCATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fill_fieldcatalog .
DEFINE fieldcat_merge.
wa_fieldcat-col_pos = &1.
wa_fieldcat-fieldname = &2.
if wa_fieldcat-fieldname = 'G_ICON'.
wa_fieldcat-icon = 'X'. " Display the field as ICON
wa_fieldcat-seltext_m = 'Occupancy Status'. " Column Header
wa_fieldcat-inttype = 'C'.
wa_fieldcat-outputlen = '20'.
elseif wa_fieldcat-fieldname = 'G_ICON1'.
wa_fieldcat-icon = 'X'. " Display the field as ICON
wa_fieldcat-seltext_m = 'Booking Status'. " Column Header
wa_fieldcat-inttype = 'C'.
wa_fieldcat-outputlen = '30'.
elseif wa_fieldcat-fieldname = 'ZSEATS'.
wa_fieldcat-seltext_m = 'Max. Seats'.
elseif wa_fieldcat-fieldname = 'ZSEATSOCC'.
wa_fieldcat-seltext_m = 'Seats Occupied'.
wa_fieldcat-outputlen = '18'.
endif.
wa_fieldcat-ref_fieldname = &3.
wa_fieldcat-ref_tabname = &4.
append wa_fieldcat to it_fieldcat.
clear wa_fieldcat.
END-OF-DEFINITION.
fieldcat_merge: '1' 'G_ICON' '' '',
'2' 'CARRID' 'CARRID' 'ZTRAFFICLIGHTS',
'3' 'FDATE' 'FDATE' 'ZTRAFFICLIGHTS',
'4' 'FPRICE' 'FPRICE' 'ZTRAFFICLIGHTS',
'5' 'CURRENCY' 'CURRENCY' 'ZTRAFFICLIGHTS',
'6' 'G_ICON1' '' '', "Second Icon field
'7' 'ZSEATS' 'ZSEATS' 'ZTRAFFICLIGHTS',
'8' 'ZSEATSOCC' 'ZSEATSOCC' 'ZTRAFFICLIGHTS'.
ENDFORM. " FILL_FIELDCATALOG
*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM display_alv .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
i_callback_program = sy-repid
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
* IS_LAYOUT =
it_fieldcat = it_fieldcat
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP = 0
* I_HTML_HEIGHT_END = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = it_final
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM. " DISPLAY_ALV
Output:
Here Occupancy Status and Booking Status are two traffic lights icon columns in ALV display.
You can also assign Tooltip to a traffic light.
Use below line of code:
"CONCATENATE '@09\Q' 'Filling Fast' '@' INTO wa_final-g_icon1."
@09@ is the internal number for traffic light.
\Q: use to concatenate text as tooltip to icon.