Home > ABAP, ALV > ALV Tips – Display column as Icon

ALV Tips – Display column as Icon

December 30th, 2009 Leave a comment Go to comments

We may need to display traffic light or some other icons in the ALV output to highlight some lines. Here is an example.

To use icons, you need to use types-pool: icon or include the include file <ICON> in the program.

PROGRAM sapbc405_alvd_merge_icon .
TYPES: BEGIN OF t_con,
  carrid LIKE sflight-carrid,
  connid LIKE sflight-connid,
  fldate LIKE sflight-fldate,
  price LIKE sflight-price,
  currency LIKE sflight-currency,
  planetype LIKE sflight-planetype,
  seatsmax LIKE sflight-seatsmax,
  seatsocc LIKE sflight-seatsocc,
  paymentsum LIKE sflight-paymentsum,
  free_seats LIKE sflight-seatsmax,
  icon_name(30),
END OF t_con.
 
* OK code handling
DATA: ok_code LIKE sy-ucomm,
      save_ok_code LIKE sy-ucomm.
 
* Data, which will be displayed in the alv Control
DATA: gt_outtab TYPE STANDARD TABLE OF t_con,
      wa_outtab TYPE t_con.
 
* Field position
DATA: pos_free TYPE i VALUE 10,
      pos_icon TYPE i VALUE 11.
 
* Fieldcatalog
DATA: gt_fieldcat TYPE lvc_t_fcat,
      wa_fieldcat LIKE LINE OF gt_fieldcat.
 
* CONTROLS
DATA: g_custom_container TYPE REF TO cl_gui_custom_container,
      alv_grid TYPE REF TO cl_gui_alv_grid.
* Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK connection WITH FRAME.
  SELECT-OPTIONS: so_car FOR wa_outtab-carrid.
SELECTION-SCREEN END OF BLOCK connection.
*&;———————————————————————*
*&; Modulpool SAPBC405_ALVD_MERGE_ICON *
*&; *
*&;———————————————————————*
*&; *
*&; *
*&;———————————————————————*
INCLUDE bc405_alvd_merge_icontop.
INCLUDE .
*&;———————————————————————*
*&; Event START-OF-SELECTION
*&;———————————————————————*
START-OF-SELECTION.
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF wa_outtab
WHERE carrid IN so_car.
wa_outtab-free_seats = wa_outtab-seatsmax – wa_outtab-seatsocc.
 
**Below statement is used to transfer the icon name to the field
IF wa_outtab-free_seats GT 0.
   wa_outtab-icon_name = icon_green_light.
ELSE.
  wa_outtab-icon_name = icon_red_light.
ENDIF.
APPEND wa_outtab TO gt_outtab.
ENDSELECT.
CALL SCREEN 100.
*&;———————————————————————*
*&; Module STATUS_0100 OUTPUT
*&;———————————————————————*
* text
*———————————————————————-*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'S100'.
SET TITLEBAR 'T100'.
ENDMODULE. " STATUS_0100 OUTPUT
 
*&;———————————————————————*
*&; Module EXIT INPUT
*&;———————————————————————*
* text
*———————————————————————-*
MODULE exit INPUT.
LEAVE PROGRAM.
ENDMODULE. " EXIT INPUT
 
*&;———————————————————————*
*&; Module USER_COMMAND_0100 INPUT
*&;———————————————————————*
* text
*———————————————————————-*
MODULE user_command_0100 INPUT.
MOVE ok_code TO save_ok_code.
CLEAR ok_code.
CASE save_ok_code.
  WHEN 'BACK'.
    CALL METHOD g_custom_container->;free.
    LEAVE TO SCREEN 0.
  WHEN 'EXIT'.
    CALL METHOD g_custom_container->;free.
    LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
 
*&;———————————————————————*
*&; Module CREATE_OBJECTS OUTPUT
*&;———————————————————————*
* text
*———————————————————————-*
MODULE create_objects OUTPUT.
 
IF g_custom_container IS INITIAL.
CREATE OBJECT g_custom_container
EXPORTING container_name = 'MY_CONTROL_AREA'.
 
CREATE OBJECT alv_grid
EXPORTING i_parent = g_custom_container.
ENDIF.
 
ENDMODULE. " CREATE_OBJECTS OUTPUT
*&;———————————————————————*
*&; Module TRANSFER_DATA OUTPUT
*&;———————————————————————*
* text
*———————————————————————-*
MODULE transfer_data OUTPUT.
 
CALL METHOD alv_grid->;set_table_for_first_display
EXPORTING i_structure_name = 'SFLIGHT'
CHANGING it_outtab = gt_outtab
it_fieldcatalog = gt_fieldcat.
 
ENDMODULE. " TRANSFER_DATA OUTPUT
*&;———————————————————————*
*&; Module FIELD_CATALOG OUTPUT
*&;———————————————————————*
* text
*———————————————————————-*
MODULE field_catalog OUTPUT.
 
* Attributes of the additional fields, which is not included
* in the DIC sructure SFLIGHT
 
CLEAR wa_fieldcat.
* Name and types
wa_fieldcat-fieldname = 'FREE_SEATS'.
wa_fieldcat-ref_table = 'SFLIGHT'.
wa_fieldcat-ref_field = 'SEATSMAX'.
* Texts and position
wa_fieldcat-coltext = text-001.
wa_fieldcat-seltext = text-001.
wa_fieldcat-col_pos = pos_free.
APPEND wa_fieldcat TO gt_fieldcat.
 
CLEAR wa_fieldcat.
* Name and types
wa_fieldcat-fieldname = 'ICON_NAME'.
wa_fieldcat-icon = 'X'. " Displayed as Icon
* Texts and position
wa_fieldcat-coltext = text-001.
wa_fieldcat-seltext = text-001.
wa_fieldcat-col_pos = pos_icon.
APPEND wa_fieldcat TO gt_fieldcat.
ENDMODULE. " FIELD_CATALOG OUTPUT
  1. December 31st, 2009 at 00:56 | #1

    Great post! I have a online Icons database – Not 100% up to date, but still helpful. http://www.home4sap.com/iconsdb.shtml.

    Please keep up the great work!

  2. December 31st, 2009 at 21:28 | #2

    @Jon D.
    Hi Jon,

    I checked the icons database.It is very useful to find wanted icons.Thanks for sharing it

    Thanks,
    Andy

  1. No trackbacks yet.