1)颜色含义
1:海蓝;2:浅清;3:黄色;4:浅蓝;5:青色;6:红色;7:橙色。
(1)首位为主颜色;
(2)次位为辅助颜色;
(3)末位为0时,表示首位数字表为表格的底色;
末位为1时,则表示以1为底色,首位数字则表为表格字体的颜色;
末位为其它颜色时,则表示底色为ALV的默认颜色;
以上三条是本人大概总结出来的规律,本人发现的现象基本如上,但也不一定全部正确。
其中C200与系统标准ALV底色比较相似;C410与系统标准关键字颜色比较相似。
2)列颜色:
在 slis_t_fieldcat_alv-emphasize 中,写入需要的颜色代码。
Eg:
DATA: fc TYPE slis_t_fieldcat_alv WITH HEADER LINE.
fc-tabname = 'ITAB'.
fc-fieldname = 'COL'. "列名
fc-emphasize = 'C100'."颜色
append fc.
设置ALV字段fieldcat属性:i_fieldcat-emphasize = &3. "颜色
把fieldcat属性写入属性内表allfields:alv_append_field 'WERKS' '工厂' 'C110'.
C110:为颜色代码;
data: i_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE
i_fieldcat-emphasize = 'C100'. "颜色代码
3)行颜色:
(1)在内表itab定义一个字段style来存储颜色;loop内表itab,在需要显示颜色的行为其值赋为颜色'C110';
(2)在定义alv显示格式定义:
设置ALV显示layout属性:i_layout-info_fieldname = 'STYLE',STYLE为内表定义的存储颜色的列名。
a.在要输出的 itab 中,加入 color 列,类型为C(4),记录需要的颜色代码。
b.设置 layout。alv_layout-info_fieldname = 'COLOR'.
Eg: DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE itab.
DATA: color(4) TYPE c.
DATA: END OF itab.
DATA: alv_layout TYPE slis_layout_alv.
"内表赋值
itab-matnr = '123'.
itab-color = 'C200'.
APPEND itab.
alv_layout-info_fieldname = 'COLOR'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout = alv_layout
TABLES
it_outtab = itab.
4)单元格颜色
单元格颜色属性:LVC_S_SCOL(类方式) ,该属性为一结构,含三个字段:FNAME(30)、COLOR(颜色结构,含三个字段:col(10)、int(3)、inv(10))、NOKEYCOL(1)(覆盖码颜色)
单元格颜色属性:slis_t_specialcol_alv,该属性为一结构,含三个字段:FIELDNAME(30)、COLOR(颜色结构,含三个字段:col(10)、int(3)、inv(10))、NOKEYCOL(1)(覆盖码颜色)
eg:
定义内表itab含字段FLDNAME type LVC_S_SCOL;
定义内表itab含字段FLDNAME type slis_t_specialcol_alv;
ALV列属性:coltab_fieldname = 'FLDNAME'.
DATA: BEGIN OF itab_test3 OCCURS 0.
INCLUDE STRUCTURE itab_test.
DATA: FLDNAME TYPE slis_t_specialcol_alv.
DATA: END OF itab_test3.
定义单元格颜色结构内表cellcolor type slis_t_specialcol_alv with header line;
循环内表,当要设置单元格颜色所在的列字段colname要设置颜色时
refresh cellcolor.
cellcolor-FIELDNAME =‘COLNAME’. "列名
cellcolor-COLOR-col =‘2’.(颜色代码)
cellcolor-COLOR-int =‘1’.
cellcolor-COLOR-inv =‘0’.
APPEND cellcolor.
CONCATENATE '2' '10' INTO itab_test3-colname. "把颜色值赋给该列
" 更新内表对应存放字段
itab_test3-fldname[] = cellcolor[].
APPEND itab_test3.
" 显示单元格颜色
DATA: alv_layout TYPE slis_layout_alv.
alv_layout-coltab_fieldname = 'FLDNAME'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout = alv_layout
TABLES
it_outtab = itab_test3.
*&---------------------------------------------------------------------*
*& Report YLPY_ALV004
*&
*&---------------------------------------------------------------------*
*& ALV 行、列、单元格颜色设置
*&
*&---------------------------------------------------------------------*
REPORT ylpy_alv004.
TYPE-POOLS: slis.
*一共定义3个内表.
*itab1 :设置列颜色,
*itab2 :设置行颜色.
*itab3 :设置单元格颜色.
DATA: BEGIN OF itab_test OCCURS 0,
col1(4) TYPE c,
col2(4) TYPE c,
col3(4) TYPE c,
col4(4) TYPE c,
col5(4) TYPE c,
col6(4) TYPE c,
END OF itab_test .
* 显示行颜色
DATA: BEGIN OF itab_test2 OCCURS 0.
INCLUDE STRUCTURE itab_test.
DATA: color(4) TYPE c. "定义行颜色字段
DATA: END OF itab_test2.
* 显示单元格颜色
DATA: BEGIN OF itab_test3 OCCURS 0.
INCLUDE STRUCTURE itab_test.
DATA: color TYPE slis_t_specialcol_alv.
DATA: END OF itab_test3.
DATA: fc TYPE slis_t_fieldcat_alv WITH HEADER LINE,
fc2 TYPE slis_t_fieldcat_alv WITH HEADER LINE,
fc3 TYPE slis_t_fieldcat_alv WITH HEADER LINE.
START-OF-SELECTION.
PERFORM create_data. "构建内表数据
END-OF-SELECTION.
PERFORM fieldcat_build. "构建列表头
PERFORM show_alv.
*&---------------------------------------------------------------------*
*& Form create_data
*&---------------------------------------------------------------------*
* 构建内表数据
*----------------------------------------------------------------------*
FORM create_data.
DATA: l_color(4) TYPE c,
l_char TYPE c,
i TYPE i.
DATA: color_wa TYPE slis_t_specialcol_alv WITH HEADER LINE."单元格颜色属性内表
REFRESH itab_test.
REFRESH itab_test2.
REFRESH itab_test3.
REFRESH fc.
REFRESH fc2.
REFRESH fc3.
WHILE sy-index < 8.
* 内表 1
itab_test-col1 = ( sy-index - 1 ) * 6 + 1.
itab_test-col2 = ( sy-index - 1 ) * 6 + 2.
itab_test-col3 = ( sy-index - 1 ) * 6 + 3.
itab_test-col4 = ( sy-index - 1 ) * 6 + 4.
itab_test-col5 = ( sy-index - 1 ) * 6 + 5.
itab_test-col6 = ( sy-index - 1 ) * 6 + 6.
APPEND itab_test.
* 内表 2
MOVE-CORRESPONDING itab_test TO itab_test2.
l_char = sy-index.
i = sy-index MOD 2. "根据奇偶数构建行颜色值
IF i = 0.
CONCATENATE 'C' l_char '01' INTO l_color.
ELSE.
CONCATENATE 'C' l_char '10' INTO l_color.
ENDIF.
itab_test2-color = l_color.
APPEND itab_test2.
* 内表 3
MOVE-CORRESPONDING itab_test TO itab_test3. "转移数据到itab_test3内表
REFRESH color_wa.
" 第一列颜色
color_wa-color-col = l_char.
color_wa-color-int = '0'.
color_wa-color-inv = '0'.
color_wa-fieldname = 'COL1'.
APPEND color_wa.
CONCATENATE l_char '00' INTO itab_test3-col1.
" 第二列颜色
color_wa-color-int = '0'.
color_wa-color-inv = '1'.
color_wa-fieldname = 'COL2'.
APPEND color_wa.
CONCATENATE l_char '01' INTO itab_test3-col2.
" 第三列颜色
color_wa-color-int = '1'.
color_wa-color-inv = '0'.
color_wa-fieldname = 'COL3'.
APPEND color_wa.
CONCATENATE l_char '10' INTO itab_test3-col3.
itab_test3-color[] = color_wa[].
APPEND itab_test3.
ENDWHILE.
ENDFORM. "create_data
************************************************************************
FORM fieldcat_build.
DATA: BEGIN OF fc_struct,
tabname(10),
fieldname(5),
seltext_m(5),
emphasize(4),
END OF fc_struct.
DEFINE ac.
clear: fc,fc2,fc_struct.
fc_struct = &1.
fc-tabname = fc_struct-tabname. "内表名
fc-fieldname = fc_struct-fieldname. "字段名
fc-seltext_m = fc_struct-seltext_m. "字段描述
fc-emphasize = fc_struct-emphasize. "列颜色
append fc.
fc2-tabname = fc_struct-tabname. "内表名
fc2-fieldname = fc_struct-fieldname. "字段名
fc2-seltext_m = fc_struct-seltext_m. "字段描述
append fc2.
if fc3-fieldname < 'COL4'. "只显示前四列
fc3-tabname = fc_struct-tabname. "内表名
fc3-fieldname = fc_struct-fieldname. "字段名
fc3-seltext_m = fc_struct-seltext_m. "字段描述
append fc3.
endif.
END-OF-DEFINITION.
" 第一个内表显示列颜色效果
ac 'ITAB_TEST COL1 COL1 C100'.
ac 'ITAB_TEST COL2 COL2 C200'.
ac 'ITAB_TEST COL3 COL3 C300'.
ac 'ITAB_TEST COL4 COL4 C400'.
ac 'ITAB_TEST COL5 COL5 C500'.
ac 'ITAB_TEST COL6 COL6 C600'.
ENDFORM. "fieldcat_build
*&---------------------------------------------------------------------*
*& Form show_alv
*&---------------------------------------------------------------------*
* 显示内表 1
*----------------------------------------------------------------------*
FORM show_alv.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = fc[]
i_callback_user_command = 'PROCESS_USER_COMMAND_1' "响应双击事件
i_save = 'A'
TABLES
t_outtab = itab_test
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. "show_alv
*&---------------------------------------------------------------------*
*& Form PROCESS_USER_COMMAND_1
*&---------------------------------------------------------------------*
* 当用户双击时显示下层内容
*----------------------------------------------------------------------*
* -->UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM process_user_command_1 USING ucomm
rs_selfield TYPE slis_selfield.
IF rs_selfield-fieldname <> space AND rs_selfield-tabindex > 0. "这句很有用
PERFORM show_alv_2.
ENDIF.
ENDFORM. "PROCESS_USER_COMMAND_1
*&---------------------------------------------------------------------*
*& Form show_alv_2
*&---------------------------------------------------------------------*
* 显示内表 2 行颜色效果
*----------------------------------------------------------------------*
FORM show_alv_2.
DATA: alv_layout TYPE slis_layout_alv.
alv_layout-info_fieldname = 'COLOR'. "显示行颜色
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = fc2[]
i_callback_user_command = 'PROCESS_USER_COMMAND_2' "下层双击事件内容
is_layout = alv_layout
i_save = 'U'
TABLES
t_outtab = itab_test2
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. "show_alv_2
*&---------------------------------------------------------------------*
*& Form PROCESS_USER_COMMAND_2
*&---------------------------------------------------------------------*
* 第三层报表内容
*----------------------------------------------------------------------*
* -->UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM process_user_command_2 USING ucomm
rs_selfield TYPE slis_selfield.
IF rs_selfield-fieldname <> space AND rs_selfield-tabindex > 0.
PERFORM show_alv_3.
ENDIF.
ENDFORM. "PROCESS_USER_COMMAND_2
*&---------------------------------------------------------------------*
*& Form show_alv_3
*&---------------------------------------------------------------------*
* 显示内表 3 单元格颜色
*----------------------------------------------------------------------*
FORM show_alv_3.
DATA: alv_layout TYPE slis_layout_alv.
alv_layout-coltab_fieldname = 'COLOR'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = fc3[]
is_layout = alv_layout
i_save = 'U'
TABLES
t_outtab = itab_test3
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. "show_alv_3