这个是SAP的教学程序,今天在网上看到有人问这样的问题,现在就把它发在网上,希望对你有点帮助。
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 <icon>.
*&---------------------------------------------------------------------*
*& 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.
**注意以下的这句话,就是把ICON中的信号灯的图标传到wa中
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