如何在ALV中显示ICON和HOTSPOT效果呢? 思路是: 在内表中加多一个field,用来设置ICON的值;然后,在fieldcatlog该field的ICON属性设为X,表明这个column用来显示ICON的;
以下是实例代码(本实例转载于: http://www.abap-tutorials.com/2009/09/10/displaying-icons-in-alv-abap/ ):
************************************************************************
* Include Programs
************************************************************************
************************************************************************
* Database Tables
************************************************************************
TABLES: kna1. "Customer Master
************************************************************************
* Types
************************************************************************
TYPE-POOLS: kkblo.
************************************************************************
* Structures
************************************************************************
DATA: BEGIN OF st_color,
color(3) TYPE c,
END OF st_color.
* Structure to hold the Icon Information
DATA: BEGIN OF st_icon,
icon(4) TYPE c,
END OF st_icon.
************************************************************************
* Internal Tables
************************************************************************
* Output Table
DATA: BEGIN OF tbl_kna1 OCCURS 0.
INCLUDE STRUCTURE st_icon. "Icon Structure
INCLUDE STRUCTURE kna1. "Customer Master Structure
INCLUDE STRUCTURE st_color. "Color Structure
DATA: END OF tbl_kna1.
************************************************************************
* Variables
************************************************************************
* ALV Field Catalog Structure
DATA: st_fieldcat TYPE slis_fieldcat_alv.
* ALV Layout Structure
DATA: st_layout TYPE slis_layout_alv.
DATA: fieldname(30) TYPE c,
g_repid LIKE sy-repid.
* ALV Field Catalog Table
DATA: tbl_fieldcat TYPE slis_t_fieldcat_alv.
************************************************************************
* Start of Selection
************************************************************************
START-OF-SELECTION.
g_repid = sy-repid.
PERFORM get_data.
************************************************************************
* End of Selection
************************************************************************
END-OF-SELECTION.
PERFORM do_fancy_stuff.
PERFORM get_layout.
PERFORM get_fieldcat.
PERFORM create_report.
*&---------------------------------------------------------------------*
*& Form CREATE_REPORT
*&---------------------------------------------------------------------*
* Learn to read the subroutine name!
*----------------------------------------------------------------------*
FORM create_report.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_interface_check = ' '
i_callback_program = g_repid
i_callback_user_command = 'PROCESS_USER_COMMANDS'
it_fieldcat = tbl_fieldcat
i_default = 'X'
i_save = ' '
is_layout = st_layout
TABLES
t_outtab = tbl_kna1
EXCEPTIONS
program_error = 1
OTHERS = 2.
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. " CREATE_REPORT
*&---------------------------------------------------------------------*
*& Form GET_FIELDCAT
*&---------------------------------------------------------------------*
* Build the Field Catalog
*----------------------------------------------------------------------*
FORM get_fieldcat.
PERFORM write_fieldcat USING 'ICON' 'TBL_KNA1' ' ' 'X' 1 '2' 'X' ' '.
PERFORM write_fieldcat USING 'KUNNR' 'TBL_KNA1' 'KNA1' 'X' 2 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'NAME1' 'TBL_KNA1' 'KNA1' ' ' 3 '10' ' ' 'X'.
PERFORM write_fieldcat USING 'STRAS' 'TBL_KNA1' 'KNA1' ' ' 4 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'TELF1' 'TBL_KNA1' 'KNA1' ' ' 5 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'ORT01' 'TBL_KNA1' 'KNA1' ' ' 6 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'PSTLZ' 'TBL_KNA1' 'KNA1' ' ' 7 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'SORTL' 'TBL_KNA1' 'KNA1' ' ' 8 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'ERNAM' 'TBL_KNA1' 'KNA1' ' ' 9 ' ' ' ' ' '.
PERFORM write_fieldcat USING 'SPRAS' 'TBL_KNA1' 'KNA1' ' ' 10 ' ' ' ' ' '.
ENDFORM. " GET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form WRITE_FIELDCAT
*&---------------------------------------------------------------------*
* Write the Field Catalog data to the Field Catalog Table
*----------------------------------------------------------------------*
* -->name Field name
* -->tab Table name
* -->st Structure Name
* -->key Is this field a Key?
* -->pos Position Number
* -->length Field Length
* -->icon Display as Icon
* -->hot Hotspot
*----------------------------------------------------------------------*
FORM write_fieldcat USING name tab st key pos length icon hot.
st_fieldcat-fieldname = name.
st_fieldcat-tabname = tab.
st_fieldcat-ref_tabname = st.
st_fieldcat-key = key.
st_fieldcat-col_pos = pos.
st_fieldcat-outputlen = length.
st_fieldcat-icon = icon.
st_fieldcat-hotspot = hot.
APPEND st_fieldcat TO tbl_fieldcat.
CLEAR st_fieldcat.
ENDFORM. " WRITE_FIELDCAT
*&---------------------------------------------------------------------*
*& Form PROCESS_USER_COMMANDS
*&---------------------------------------------------------------------*
* Interactive Reporting Commands
*----------------------------------------------------------------------*
FORM process_user_commands USING syst-ucomm LIKE syst-ucomm
selfield TYPE slis_selfield.
* This subroutine is called when there is user interaction in the output
* In this case if the user double clicks the Customer Number then the
* program will call transaction XD03 and display the Customer Master
* Data
CASE syst-ucomm.
WHEN '&IC1'.
* get cursor field fieldname.
READ TABLE tbl_kna1 INDEX selfield-tabindex.
SET PARAMETER ID 'KUN' FIELD tbl_kna1-kunnr.
CALL TRANSACTION 'XD03' AND SKIP FIRST SCREEN.
ENDCASE.
ENDFORM. " PROCESS_USER_COMMANDS
*&---------------------------------------------------------------------*
*& Form GET_LAYOUT
*&---------------------------------------------------------------------*
* set the layout of the ALV.
* add color to the row?
*----------------------------------------------------------------------*
FORM get_layout.
st_layout-info_fieldname = 'COLOR'.
st_layout-colwidth_optimize = 'X'.
ENDFORM. " GET_LAYOUT
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* Get some data to play with
*----------------------------------------------------------------------*
FORM get_data.
SELECT * FROM kna1 INTO CORRESPONDING FIELDS OF TABLE tbl_kna1
UP TO 30 ROWS.
ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form do_fancy_stuff
*&---------------------------------------------------------------------*
* Do some fancy pants stuff for example changing the color of
* lines and adding icons
*----------------------------------------------------------------------*
FORM do_fancy_stuff.
* Here we will demonstrate changing the color of ALV Record lines as
* well as displaying Icons
LOOP AT tbl_kna1.
* All records where NAME1 begins with 'M', will be displayed in Bluish
* Green
IF tbl_kna1-name1(1) EQ 'M'.
tbl_kna1-color = 'C41'. "Bluish Green
MODIFY tbl_kna1 TRANSPORTING color.
ENDIF.
* All records with no TELF1 will be displayed in White and have a
* Warning Icon
IF tbl_kna1-telf1 IS INITIAL.
tbl_kna1-color = 'C00'. "White
tbl_kna1-icon = '@AH@'. "Warning Icon
MODIFY tbl_kna1 TRANSPORTING icon color.
ENDIF.
ENDLOOP.
ENDFORM. " do_fancy_stuff