ALV Icon 设置

如何在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(3TYPE c,
END OF st_color.
* Structure to hold the Icon Information
DATA: BEGIN OF st_icon,
      icon(4TYPE 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(30TYPE 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' '2' 'X' ' '.
  PERFORM write_fieldcat USING 'KUNNR' 'TBL_KNA1' 'KNA1' 'X' ' ' ' ' ' '.
  PERFORM write_fieldcat USING 'NAME1' 'TBL_KNA1' 'KNA1' ' ' '10' ' ' 'X'.
  PERFORM write_fieldcat USING 'STRAS' 'TBL_KNA1' 'KNA1' ' ' ' ' ' ' ' '.
  PERFORM write_fieldcat USING 'TELF1' 'TBL_KNA1' 'KNA1' ' ' ' ' ' ' ' '.
  PERFORM write_fieldcat USING 'ORT01' 'TBL_KNA1' 'KNA1' ' ' ' ' ' ' ' '.
  PERFORM write_fieldcat USING 'PSTLZ' 'TBL_KNA1' 'KNA1' ' ' ' ' ' ' ' '.
  PERFORM write_fieldcat USING 'SORTL' 'TBL_KNA1' 'KNA1' ' ' ' ' ' ' ' '.
  PERFORM write_fieldcat USING 'ERNAM' 'TBL_KNA1' 'KNA1' ' ' ' ' ' ' ' '.
  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(1EQ '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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值