ABAP ALV Grid 色彩:ALV 列表中的表行背景色
【摘要】 ABAP 开发人员可以在使用 ALV 布局 info_fname 属性的多色 ALV 网格颜色替代中找到有关如何在 ALV 列表上显示数据行时使用交替 ALV 网格颜色的教程。如果是针对某一列的颜色,可以使用 emphasize要在 F4 帮助弹出窗口中突出显示不同颜色的 ALV 行,只需填写正确的颜色键即可。当然,在这种方法中,在 ABAP 代码中斑马纹布局样式并不用于 ALV 网格表行的...
ABAP 开发人员可以在使用 ALV 布局 info_fname
属性的多色 ALV 网格颜色替代中找到有关如何在 ALV 列表上显示数据行时使用交替 ALV 网格颜色的教程。
如果是针对某一列的颜色,可以使用 emphasize
要在 F4 帮助弹出窗口中突出显示不同颜色的 ALV 行,只需填写正确的颜色键即可。
当然,在这种方法中,在 ABAP 代码中斑马纹布局样式并不用于 ALV 网格表行的背景颜色。
因为斑马线布局只有两种颜色交替排列 ALV 列表中的每行或每条记录。
但是在提到的 ABAP ALV 教程中,我们在 ALV 网格显示屏上有相同的颜色对。
以下对必须具有不同的网格颜色。
在本 ALV 教程中,我们将提前一步,以便为网格着色和为 ALV 列表示例着色。
下面是 ALV 网格颜色示例程序的 ABAP 报告源代码,其中 ALV 列表在每个 ALV 网格行中具有不同的表行背景颜色。
在下面的 ABAP 报告中,ABAP 开发人员将看到分配给 ALV 网格布局结构的 info_fname 属性的颜色网格代码,
这些 ALV 网格颜色代码是:
- C110
- C210
- C310
- C410
- C510
- C610
- C710
- C810
- C010
创建方式
在输出内部表中创建一个字段来存储颜色键。内部输出表字段的类型必须为 CHAR(4)
。
REPORT ZABAPColorALVGridRows .
TYPES:
BEGIN OF gty_vbak,
vbeln TYPE vbeln,
line_color(4) TYPE c,
END OF gty_vbak.
DATA :
lv_i TYPE i,
lv_c(1) TYPE c,
lv_doubl(1) TYPE c,
lv_color(4) TYPE c,
gs_vbak TYPE gty_vbak,
gt_vbak TYPE TABLE OF gty_vbak.
DATA :
g_container TYPE scrfname VALUE 'CC_CONTAINER',
g_custom_container TYPE REF TO cl_gui_custom_container,
g_grid TYPE REF TO cl_gui_alv_grid,
gs_layout TYPE lvc_s_layo,
gs_fieldcatalog TYPE lvc_s_fcat OCCURS 0,
gv_fcat LIKE LINE OF gs_fieldcatalog,
ok_code LIKE sy-ucomm.
START-OF-SELECTION.
SELECT vbeln INTO TABLE gt_vbak FROM vbak UP TO 20 ROWS.
LOOP AT gt_vbak INTO gs_vbak.
lv_i = sy-tabix MOD 9.
lv_c = lv_i.
CONCATENATE 'C' lv_c '10' INTO lv_color.
gs_vbak-line_color = lv_color.
MODIFY gt_vbak FROM gs_vbak.
ENDLOOP.
CALL SCREEN 100.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE ok_code .
WHEN 'BACK' OR 'EXIT' OR 'CNCL'.
LEAVE PROGRAM.
WHEN OTHERS.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS'.
SET TITLEBAR 'TITLE'.
IF g_custom_container IS INITIAL.
" Create CONTAINER object with reference to container name on the screen
CREATE OBJECT g_custom_container EXPORTING container_name = g_container.
" Create GRID object with reference to parent name
CREATE OBJECT g_grid EXPORTING i_parent = g_custom_container.
PERFORM u_preparefieldcatalog.
gs_layout-info_fname = 'LINE_COLOR'.
" SET_TABLE_FOR_FIRST_DISPLAY
CALL METHOD g_grid->set_table_for_first_display
EXPORTING
is_layout = gs_layout
CHANGING
it_fieldcatalog = gs_fieldcatalog
it_outtab = gt_vbak.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Form u_preparefieldcatalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM u_preparefieldcatalog .
CLEAR gv_fcat.
gv_fcat-fieldname = 'VBELN'.
gv_fcat-tabname = 'VBAP'.
gv_fcat-coltext = 'Order No.'.
INSERT gv_fcat INTO TABLE gs_fieldcatalog.
ENDFORM. "u_preparefieldcatalog
报表示例
- 在 SE38 事务码中创建 ABAP 程序
- 添加字段
CELLCOLOR
(type LVC_T_SCOL
) 到 fieldcat - 根据你的业务要求,填充字段
CELLCOLOR
- 将
CELLCOLOR
分配给ALV Grid Layout
结构的COLTAB_FIELDNAME
- 使用函数模块
REUSE_ALV_GRID_DISPLAY
创建一个 ALV 报表
*&-------------------------------------------------------------*
*& Report ZDEMO_ALVGRID *
*& *
*&-------------------------------------------------------------*
*& *
*& Example of a simple ALV Grid Report *
*& ................................... *
*& *
*& The basic ALV grid, Enhanced to display each row in a *
*& different color *
*&-------------------------------------------------------------*
REPORT zdemo_alvgrid.
TABLES: ekko. " Table Declarations
TYPE-POOLS: slis. " ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF t_ekko,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
statu TYPE ekpo-statu,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
peinh TYPE ekpo-peinh,
line_color(4) type c, "Used to store row color attributes
END OF t_ekko.
DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
wa_ekko TYPE t_ekko.
*ALV data declarations
DATA: fieldcatalog type slis_t_fieldcat_alv with header line,
gd_tab_group type slis_t_sp_group_alv,
gd_layout type slis_layout_alv,
gd_repid like sy-repid.
************************************************************************
*Start-of-selection.
START-OF-SELECTION.
perform data_retrieval.
perform build_fieldcatalog.
perform build_layout.
perform display_alv_report.
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
* Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
form build_fieldcatalog.
* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you more control of the final product.
* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
* I.e. Field type may be required in-order for
* the 'TOTAL' function to work.
fieldcatalog-fieldname = 'EBELN'.
fieldcatalog-seltext_m = 'Purchase Order'.
fieldcatalog-col_pos = 0.
fieldcatalog-outputlen = 10.
fieldcatalog-emphasize = 'X'.
fieldcatalog-key = 'X'.
* fieldcatalog-do_sum = 'X'.
* fieldcatalog-no_zero = 'X'.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'EBELP'.
fieldcatalog-seltext_m = 'PO Item'.
fieldcatalog-col_pos = 1.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'STATU'.
fieldcatalog-seltext_m = 'Status'.
fieldcatalog-col_pos = 2.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'AEDAT'.
fieldcatalog-seltext_m = 'Item change date'.
fieldcatalog-col_pos = 3.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'MATNR'.
fieldcatalog-seltext_m = 'Material Number'.
fieldcatalog-col_pos = 4.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'MENGE'.
fieldcatalog-seltext_m = 'PO quantity'.
fieldcatalog-col_pos = 5.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'MEINS'.
fieldcatalog-seltext_m = 'Order Unit'.
fieldcatalog-col_pos = 6.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'NETPR'.
fieldcatalog-seltext_m = 'Net Price'.
fieldcatalog-col_pos = 7.
fieldcatalog-outputlen = 15.
fieldcatalog-datatype = 'CURR'.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
fieldcatalog-fieldname = 'PEINH'.
fieldcatalog-seltext_m = 'Price Unit'.
fieldcatalog-col_pos = 8.
append fieldcatalog to fieldcatalog.
clear fieldcatalog.
endform. " BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*& Form BUILD_LAYOUT
*&---------------------------------------------------------------------*
* Build layout for ALV grid report
*----------------------------------------------------------------------*
form build_layout.
gd_layout-no_input = 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-totals_text = 'Totals'(201).
* Set layout field for row attributes(i.e. color)
gd_layout-info_fieldname = 'LINE_COLOR'.
* gd_layout-totals_only = 'X'.
* gd_layout-f2code = 'DISP'. "Sets fcode for when double
* "click(press f2)
* gd_layout-zebra = 'X'.
* gd_layout-group_change_edit = 'X'.
* gd_layout-header_text = 'helllllo'.
endform. " BUILD_LAYOUT
*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
* Display report using ALV grid
*----------------------------------------------------------------------*
form display_alv_report.
gd_repid = sy-repid.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = gd_repid
* i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM
* i_callback_user_command = 'USER_COMMAND'
* i_grid_title = outtext
is_layout = gd_layout
it_fieldcat = fieldcatalog[]
* it_special_groups = gd_tabgroup
* IT_EVENTS = GT_XEVENTS
i_save = 'X'
* is_variant = z_template
tables
t_outtab = it_ekko
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. " DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*& Form DATA_RETRIEVAL
*&---------------------------------------------------------------------*
* Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
form data_retrieval.
data: ld_color(1) type c.
select ebeln ebelp statu aedat matnr menge meins netpr peinh
up to 10 rows
from ekpo
into table it_ekko.
*Populate field with color attributes
loop at it_ekko into wa_ekko.
* Populate color variable with colour properties
* Char 1 = C (This is a color property)
* Char 2 = 3 (Color codes: 1 - 7)
* Char 3 = Intensified on/off ( 1 or 0 )
* Char 4 = Inverse display on/off ( 1 or 0 )
* i.e. wa_ekko-line_color = 'C410'
ld_color = ld_color + 1.
* Only 7 colours so need to reset color value
if ld_color = 8.
ld_color = 1.
endif.
concatenate 'C' ld_color '10' into wa_ekko-line_color.
* wa_ekko-line_color = 'C410'.
modify it_ekko from wa_ekko.
endloop.
endform. " DATA_RETRIEVAL
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)