ABAP ALV 颜色设置(行,列,单元格)

时间:2022-12-30 08:11:00

BCALV_EDIT_03

http://blog.sina.com.cn/s/blog_a87b19300102who3.html

关于ALV表格颜色,这种需求在项目中会经常用到。

  • 列颜色

列的颜色很简单,在构成ALV字段的FIELDCAT内表中有一个字段是EMPHASIZE,将一个char型4位的颜色代码分配到FIELDCAT内表这个字段即可。

1 LW_FCAT-EMPHASIZE = 'C710'. "设置字段的颜色
2
3 MODIFY IT_FIELDCAT FROM LW_FCAT
4 TRANSPORTING EMPHASIZE
5 WHERE FIELDNAME = 'ESLRY'.
  • 行颜色

1. 在构成ALV数据的内表中,添加一个char型4位的字段(CLR),用来记录颜色代码

1 TABLES ZEMP_TEST.
2
3 TYPES: BEGIN OF TY_ZEMP.
4 INCLUDE STRUCTURE ZEMP_TEST.
5 TYPES: CLR TYPE CHAR4,"可以控制行颜色
6 END OF TY_ZEMP.
7
8 DATA: IT_ZEMP TYPE STANDARD TABLE OF TY_ZEMP,
9 IW_ZEMP TYPE TY_ZEMP.

2. 循环内表,设置颜色代码

1   LOOP AT IT_ZEMP INTO IW_ZEMP.
2 "员工编号为‘3的行,颜色为红色
3 IF IW_ZEMP-EMPID = '3'.
4 IW_ZEMP-CLR = 'C610'.
5 MODIFY IT_ZEMP FROM IW_ZEMP.
6 CLEAR IW_ZEMP.
7 ENDIF.
8 ENDLOOP.

3. LAYOUT结构中INFO_FIELDNAME的值指定为 CLR

  IW_LAYOUT-INFO_FIELDNAME = 'CLR'.  "行颜色代码的字段
  • 单元格颜色

单元格颜色的设置与行颜色设计基本一致

1. 在构成ALV数据的内表中,添加一个内表 CellColor,用来记录颜色代码,需要设置颜色的字段名

1 TYPES: BEGIN OF TY_ZEMP.
2 INCLUDE STRUCTURE ZEMP_TEST.
3 TYPES: CLR TYPE CHAR4,
4 CELLCOLOR TYPE LVC_T_SCOL, "单元格颜色
5 END OF TY_ZEMP.

CellColor的结构:FNAME        ALV 控制: 内部表字段的字段名称         

COLOR        ALV 控制: 颜色代码     COLOR是一个结构:COL      ALV 控制: 颜色      

INT      ALV 控制: 强化    1/0                                    

INV      ALV 控制: 相反    1/0    设置颜色是前景,或者是背景         

NOKEYCOL  ALV 控制: 覆盖码颜色

2. 循环内表,设置颜色代码

   LOOP AT IT_ZEMP INTO IW_ZEMP.

      "员工编号为‘3’的行,颜色为红色
IF IW_ZEMP-EMPID = ''.
IW_ZEMP-CLR = 'C610'. MODIFY IT_ZEMP FROM IW_ZEMP.
CLEAR IW_ZEMP. ENDIF. IF IW_ZEMP-EMPID = ''.
IW_CELLCOLOR-FNAME = 'ENAME'.
"员工编号为‘2’的行的‘ENAME’字段颜色为 黄色
IW_CELLCOLOR-COLOR-COL = .
IW_CELLCOLOR-COLOR-INT = .
IW_CELLCOLOR-COLOR-INV = . APPEND IW_CELLCOLOR TO IW_ZEMP-CELLCOLOR.
MODIFY IT_ZEMP FROM IW_ZEMP.
CLEAR IW_ZEMP. ENDIF.
ENDLOOP.

3. LAYOUT结构中COLTAB_FIELDNAME的值指定为CELLCOLOR

  IW_LAYOUT-COLTAB_FIELDNAME = 'CELLCOLOR'. "单元格颜色内表字段

最后在调用ALV函数时,设置layout,fieldcat参数

     IS_LAYOUT                         = IW_LAYOUT
IT_FIELDCAT = IT_FIELDCAT
  • ALV工具栏

可以自定义工具栏,或者添加标准ALV工具栏,然后去除一些不需要的功能,这样可以保留标准工具栏的功能

 1   DATA:BEGIN OF TAB OCCURS 0,
2 FCODE LIKE RSMPE-FUNC,
3 END OF TAB.
4
5 TAB-FCODE = '&OL0'.APPEND TAB.
6 TAB-FCODE = '&OAD'.APPEND TAB.
7 TAB-FCODE = '&AVE'.APPEND TAB.
8
9 SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'
10 EXCLUDING TAB IMMEDIATELY.
  • 求和
LW_FCAT-DO_SUM = 'X'.  "立即求和,只有I,F,P类型的列可以求和

----------------------------------------------------------

运行效果如下:

ABAP ALV 颜色设置(行,列,单元格)

在SE38环境下的程序名输入栏输入’DEMO*’后按F4,你可以查到SAP所有的DEMO示例程序,会学到很多ABAP功能的实现方法,

输入’BCALV*’后按F4,你可以查到很多ALV示例程序。

附:ALV的颜色代码:http://blog.csdn.net/wren2004/article/details/3941551

  ABAP ALV 颜色设置(行,列,单元格)

*----------------------------------------------------------------------------------------------------------------------------------、

ABAP ALV 颜色设置(行,列,单元格)

 INCLUDE <icon>.

 INCLUDE <symbol>.

 TYPE-POOLS slis.

 TABLES spfli.

 TYPES: BEGIN OF ty_layout,

         exception(),

         checkbox,

         icon TYPE icon_d,

         symbol TYPE icon_d,

         color(),

         cell_color TYPE slis_t_specialcol_alv,

         hyperlink TYPE string,

         link_handle TYPE int4,

        END OF ty_layout.

 TYPES BEGIN OF ty_spfli.

 INCLUDE TYPE ty_layout.

 INCLUDE TYPE spfli.

 TYPES END OF ty_spfli.

 TYPES ty_tab_spfli TYPE ty_spfli OCCURS .

 PERFORM f_main.

 *&---------------------------------------------------------------------*

 *&      Form  f_main

 *&---------------------------------------------------------------------*

 *       ALV display

 *----------------------------------------------------------------------*

 FORM f_main.

   DATA: lt_spfli TYPE ty_tab_spfli,

         lt_fieldcat TYPE slis_t_fieldcat_alv,

         ls_layout TYPE slis_layout_alv,

         lt_hyperlink TYPE lvc_t_hype,

         lt_add_fieldcat TYPE slis_t_add_fieldcat.

   PERFORM get_data TABLES lt_spfli.

   PERFORM set_catalog CHANGING lt_fieldcat.

   PERFORM set_layout USING ls_layout.

   PERFORM hyperlink_build TABLES lt_hyperlink.

   PERFORM add_fieldcat_build TABLES lt_add_fieldcat.

   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

     EXPORTING

       i_callback_program = sy-repid

       i_grid_title       = 'Xin''s ALV'

       it_fieldcat        = lt_fieldcat

       it_hyperlink       = lt_hyperlink

       is_layout          = ls_layout

       it_add_fieldcat    = lt_add_fieldcat

     TABLES

       t_outtab           = lt_spfli.

 ENDFORM.                    "f_main

 *&---------------------------------------------------------------------*

 *&      Form  set_layout

 *&---------------------------------------------------------------------*

 *       text

 *----------------------------------------------------------------------*

 *      -->P_LAYOUT   text

 *----------------------------------------------------------------------*

 FORM set_layout USING p_layout TYPE slis_layout_alv.

   p_layout-zebra = 'X'.

   p_layout-colwidth_optimize = 'X'.

   p_layout-window_titlebar = 'LinHuanXin''s ALV Demo'.

   p_layout-no_hline = 'X'.

   p_layout-no_vline = 'X'.

   p_layout-info_fieldname = 'COLOR'.

   p_layout-coltab_fieldname = 'CELL_COLOR'.

   p_layout-lights_fieldname = 'EXCEPTION'.

 ENDFORM.                    "set_layout

 *&---------------------------------------------------------------------*

 *&      Form  get_data

 *&---------------------------------------------------------------------*

 *       Get the data for output

 *----------------------------------------------------------------------*

 *      -->PT_SPFLI   data internal table

 *----------------------------------------------------------------------*

 FORM get_data TABLES pt_spfli TYPE ty_tab_spfli.

   DATA: lt_cell TYPE slis_t_specialcol_alv,

         ls_cell LIKE LINE OF lt_cell,

         lwa_spfli LIKE LINE OF pt_spfli.

   SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE pt_spfli.

   LOOP AT pt_spfli INTO lwa_spfli.

     IF lwa_spfli-distid = 'KM'.

       lwa_spfli-color = 'C610'.

     ENDIF.

     IF lwa_spfli-distance < .

       CLEAR ls_cell.

       REFRESH lt_cell.

       ls_cell-fieldname = 'DISTANCE'.

       ls_cell-color-col = ''.

       ls_cell-color-int = ''.

       ls_cell-color-inv = ''.

       ls_cell-nokeycol = 'X'.

       APPEND ls_cell TO lt_cell.

       lwa_spfli-cell_color = lt_cell.

       lwa_spfli-checkbox = 'X'.

       lwa_spfli-exception = ''.

     ELSE.

       lwa_spfli-exception = ''.

     ENDIF.

     lwa_spfli-link_handle = sy-tabix.

     lwa_spfli-hyperlink = 'www.163.com'.

     lwa_spfli-icon = icon_detail.

     lwa_spfli-symbol = sym_folder.

     MODIFY pt_spfli FROM lwa_spfli.

   ENDLOOP.

 ENDFORM.                    "get_data

 *&---------------------------------------------------------------------*

 *&      Form  add_fieldcat_build

 *&---------------------------------------------------------------------*

 *       text

 *----------------------------------------------------------------------*

 *      -->PT_FIELDCAT  text

 *----------------------------------------------------------------------*

 FORM add_fieldcat_build TABLES pt_fieldcat TYPE slis_t_add_fieldcat.

   DATA lwa_fieldcat LIKE LINE OF pt_fieldcat.

   lwa_fieldcat-fieldname = 'HYPERLINK'.

   lwa_fieldcat-web_field = 'LINK_HANDLE'.

   APPEND lwa_fieldcat TO pt_fieldcat.

 ENDFORM.                    "get_data

 *&---------------------------------------------------------------------*

 *&      Form  hyperlink_build

 *&---------------------------------------------------------------------*

 *       text

 *----------------------------------------------------------------------*

 *      -->PT_HYPERLINK  text

 *----------------------------------------------------------------------*

 FORM hyperlink_build TABLES pt_hyperlink TYPE lvc_t_hype.

   DATA lwa_hyperlink LIKE LINE OF pt_hyperlink.

   DO  TIMES.

     lwa_hyperlink-handle = sy-index.

     lwa_hyperlink-href   = 'www.sina.com.cn'.

     APPEND lwa_hyperlink TO pt_hyperlink.

   ENDDO.

   DO  TIMES.

     lwa_hyperlink-handle =  + sy-index.

     lwa_hyperlink-href   = 'www.163.com'.

     APPEND lwa_hyperlink TO pt_hyperlink.

   ENDDO.

 ENDFORM.                    "hyperlink_build

 *&---------------------------------------------------------------------*

 *&      Form  set_catalog

 *&---------------------------------------------------------------------*

 *       set fieldcatalog for alv output

 *----------------------------------------------------------------------*

 FORM set_catalog CHANGING pt_fieldcat TYPE slis_t_fieldcat_alv.

   DATA lwa_fieldcat TYPE slis_fieldcat_alv.

   "macro

   DEFINE add_field.

     clear lwa_fieldcat.

     lwa_fieldcat-fieldname = &.

     lwa_fieldcat-seltext_l = &.

     lwa_fieldcat-emphasize = &.

     lwa_fieldcat-key = &.

     lwa_fieldcat-fix_column = &.

     append lwa_fieldcat to pt_fieldcat.

   END-OF-DEFINITION.

   "add field catalog

   add_field 'MANDT'     'Client'                 ''     'X'.

   add_field 'CARRID'    'Airline Code'           ''     'X'.

   add_field 'CONNID'    'Fight Number'           ''     'X'.

   add_field 'COUNTRYFR' 'Country From'           ''     ''.

   add_field 'CITYFROM'  'City From'              ''     ''.

   add_field 'AIRPFROM'  'Airpart From'           ''     ''.

   add_field 'COUNTRYTO' 'Country To'             'C510' ''.

   add_field 'CITYTO'    'City To'                ''     ''.

   add_field 'AIRPTO'    'Airpart To'             ''     ''.

   add_field 'FLTIME'    'Fly Time'               ''     ''.

   add_field 'DEPTIME'   'Dept Time'              ''     ''.

   add_field 'ARRTIME'   'Arrive Time'            ''     ''.

   add_field 'DISTANCE'  'Distance'               ''     ''.

   add_field 'DISTID'    'Mass unit of distance'  ''     ''.

   add_field 'FLTYPE'    'Flight type'            ''     ''.

   add_field 'PERIOD'    'Arrival n day(s) later' ''     ''.

   add_field 'HYPERLINK' 'HYPERLINK'              ''     ''.

   "checkbox

   CLEAR lwa_fieldcat.

   lwa_fieldcat-fieldname = 'CHECKBOX'.

   lwa_fieldcat-seltext_l = 'Checkbox'.

   lwa_fieldcat-checkbox = 'X'.

   lwa_fieldcat-fix_column = 'X'.

   APPEND lwa_fieldcat TO pt_fieldcat.

   "icon

   CLEAR lwa_fieldcat.

   lwa_fieldcat-fieldname = 'ICON'.

   lwa_fieldcat-seltext_l = 'Icon'.

   lwa_fieldcat-icon = 'X'.

   APPEND lwa_fieldcat TO pt_fieldcat.

   "symbol

   CLEAR lwa_fieldcat.

   lwa_fieldcat-fieldname = 'SYMBOL'.

   lwa_fieldcat-seltext_l = 'Symbol'.

   lwa_fieldcat-symbol = 'X'.

   APPEND lwa_fieldcat TO pt_fieldcat.

 ENDFORM.                    "set_catalog
 BUTN_TYPE                  按钮类型
可用的按钮类型:
Button(normal)
Menu and default button
Menu
分割符
Radio button
Checkbox
Menu entry