动态内表用ALV输出时能不能再定义每个FIELD的颜色呢?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态内表用ALV输出时能不能再定义每个FIELD的颜色呢?相关的知识,希望对你有一定的参考价值。
动态内表用ALV输出时能不能再定义每个FIELD的颜色呢?我这有个需求,输出的列要动态生成,且要用ALV输出,还要根据表中数据的为同定义不同的颜色,动态内表ALV输出没问题,ALV输出是第个单元格不同的颜色表事也没有问题,但是两者结合起来就是做不出来.请教各位高手了..........有见过系统例子的也来回一个吧..
参考技术A Hi , This is the code that u can try..i got from one of the SAP documents... ABAP Code Sample that uses dynamic programming techniques to build a dynamic internal table for display in an ALV Grid with Cell Coloring. Code Sample REPORT zcdf_dynamic_table. * Dynamic ALV Grid with Cell Coloring. * Build a field catalog dynamically and provide the ability to color * the cells. * To test, copy this code to any program name and create screen 100 * as described in the comments. After the screen is displayed, hit * enter to exit the screen. * Tested in 4.6C and 6.20 * Charles Folwell - email@removed - Feb 2, 2005 DATA: r_dyn_table TYPE REF TO data, r_wa_dyn_table TYPE REF TO data, r_dock_ctnr TYPE REF TO cl_gui_docking_container, r_alv_grid TYPE REF TO cl_gui_alv_grid, t_fieldcat1 TYPE lvc_t_fcat, "with cell color t_fieldcat2 TYPE lvc_t_fcat, "without cell color wa_fieldcat LIKE LINE OF t_fieldcat1, wa_cellcolors TYPE LINE OF lvc_t_scol, wa_is_layout TYPE lvc_s_layo. FIELD-SYMBOLS: TYPE STANDARD TABLE, TYPE ANY, TYPE lvc_t_scol, TYPE ANY. START-OF-SELECTION. * Build field catalog based on your criteria. wa_fieldcat-fieldname = 'FIELD1'. wa_fieldcat-inttype = 'C'. wa_fieldcat-outputlen = '10'. wa_fieldcat-coltext = 'My Field 1'. wa_fieldcat-seltext = wa_fieldcat-coltext. APPEND wa_fieldcat TO t_fieldcat1. wa_fieldcat-fieldname = 'FIELD2'. wa_fieldcat-inttype = 'C'. wa_fieldcat-outputlen = '10'. wa_fieldcat-coltext = 'My Field 2'. wa_fieldcat-seltext = wa_fieldcat-coltext. APPEND wa_fieldcat TO t_fieldcat1. * Before adding cell color table, save fieldcatalog to pass * to ALV call. The ALV call needs a fieldcatalog without * the internal table for cell coloring. t_fieldcat2[] = t_fieldcat1[]. * Add cell color table. * CALENDAR_TYPE is a structure in the dictionary with a * field called COLTAB of type LVC_T_SCOL. You can use * any structure and field that has the type LVC_T_SCOL. wa_fieldcat-fieldname = 'T_CELLCOLORS'. wa_fieldcat-ref_field = 'COLTAB'. wa_fieldcat-ref_table = 'CALENDAR_TYPE'. APPEND wa_fieldcat TO t_fieldcat1. * Create dynamic table including the internal table * for cell coloring. CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = t_fieldcat1 IMPORTING ep_table = r_dyn_table EXCEPTIONS generate_subpool_dir_full = 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. * Get access to new table using field symbol. ASSIGN r_dyn_table->* TO . * Create work area for new table. CREATE DATA r_wa_dyn_table LIKE LINE OF . * Get access to new work area using field symbol. ASSIGN r_wa_dyn_table->* TO . * Get data into table from somewhere. Field names are * known at this point because field catalog is already * built. Read field names from the field catalog or use * COMPONENT in a DO loop to access the fields. A * simpler hard coded approach is used here. ASSIGN COMPONENT 'FIELD1' OF STRUCTURE TO . = 'ABC'. ASSIGN COMPONENT 'FIELD2' OF STRUCTURE TO . = 'XYZ'. APPEND TO . ASSIGN COMPONENT 'FIELD1' OF STRUCTURE TO . = 'TUV'. ASSIGN COMPONENT 'FIELD2' OF STRUCTURE TO . = 'DEF'. APPEND TO . * Color cells based on your criteria. In this example, a test on * FIELD2 is used to decide on color. LOOP AT INTO . ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table 参考技术B 怎样实现? 动态抬头的我试验过没有成功,多表的那种没有试过。如果在表中加color字段的话,动态转换后grid alv默认成数据显示了。 参考技术C types: begin of slis_specialcol_alv, fieldname type slis_fieldname, color type slis_color, nokeycol(1) type c, end of slis_specialcol_alv.ALV屏幕输出小结
2019-10-09
用 ALV 输出的基本流程:
PERFORM get_data.
PERFORM event_build.
PERFORM layout_build.
PERFORM fields.
PERFORM output_alv.
1. 定义 ALV 用到的变量.TYPE-POOLS: slis. "packageDATA: i_alv_fieldcat TYPE slis_t_fieldcat_alv.
DATA: i_alv_field TYPE slis_fieldcat_alv.
DATA: i_layout TYPE slis_layout_alv.
DATA: w_repid LIKE sy-repid.
DATA: i_events TYPE slis_t_event,
i_event_exit TYPE slis_t_event_exit,
i_list_comments TYPE slis_t_listheader, "用来填充表单标题区域
i_excluding TYPE slis_t_extab.
DATA: w_variant LIKE disvariant,
w_variant_save(1) TYPE c,
w_events LIKE LINE OF i_events,
w_list_comments LIKE LINE OF i_list_comments,i_sort TYPE STANDARD TABLE OF slis_sortinfo_alv,
w_sort LIKE LINE OF i_sort.
2. 定义INITIALIZATION部分CLEAR: w_variant.
w_repid = sy-repid.
w_variant-report = w_repid.
w_variant-username = sy-uname.
w_variant_save = ‘A‘. "All types3. 对标题区和页尾区赋值FORM event_build .CALL FUNCTION ‘REUSE_ALV_EVENTS_GET‘ "获得事件名称及 form 的内表, 后面去填补 form"
EXPORTING
i_list_type = 0
IMPORTING
et_events = i_events.
READ TABLE i_events WITH KEY name = slis_ev_top_of_page INTO w_events.
IF sy-subrc = 0.
MOVE ‘ALV_TOP_OF_PAGE‘ TO w_events-form. "将标题区数据赋值给 W_EVENTS
MODIFY i_events FROM w_events INDEX sy-tabix.
ENDIF.
READ TABLE i_events WITH KEY name = slis_ev_end_of_list INTO w_events.
IF sy-subrc = 0.
MOVE ‘ALV_END_OF_LIST‘ TO w_events-form. "将页尾数据赋值给 W_EVENTS
MODIFY i_events FROM w_events INDEX sy-tabix.
ENDIF.
READ TABLE i_events WITH KEY name = slis_ev_end_of_page INTO w_events.
IF sy-subrc = 0.
MOVE ‘ALV_END_OF_PAGE‘ TO w_events-form. "将页脚区数据赋值给 W_EVENTS.
MODIFY i_events FROM w_events INDEX sy-tabix.
ENDIF.ENDFORM.定义上面用到的子函数FORM alv_top_of_page.DATA: p_werks(40).
CLEAR: i_list_comments[].
w_list_comments-typ = ‘H‘. "H=Header, S=Selection, A=Action
w_list_comments-key = ‘‘.
w_list_comments-info = ‘USI 同一 PO 分批收料报表‘.
APPEND w_list_comments TO i_list_comments.
* w_list_comments-typ = ‘S‘. "H = Header, S = Selection, A = Action
* w_list_comments-key = ‘‘.
* CONCATENATE ‘厂别‘ ‘1000‘ INTO p_werks.
* w_list_comments-info = p_werks.
* APPEND w_list_comments TO i_list_comments.
*
* w_list_comments-typ = ‘S‘. "H = Header, S = Selection, A = Action
* w_list_comments-key = ‘‘.
* CONCATENATE ‘厂别‘ ‘2000‘ INTO p_werks.
* w_list_comments-info = p_werks.
* APPEND w_list_comments TO i_list_comments.
CALL FUNCTION ‘REUSE_ALV_COMMENTARY_WRITE‘
EXPORTING
i_logo = ‘ENJOYSAP_LOGO‘
it_list_commentary = i_list_comments.ENDFORM. "alv_top_of_page*&---------------------------------------------------------------------*4. Layout设置
*& Form alv_end_of_list
*&---------------------------------------------------------------------*
FORM alv_end_of_list.
clear: i_list_comments[].
w_list_comments-typ = ‘H‘. "H=Header, S=Selection, A=Action
w_list_comments-key = ‘‘.
w_list_comments-info = ‘页脚显示‘.
APPEND w_list_comments to i_list_comments.
call FUNCTION ‘REUSE_ALV_COMMENTARY_WRITE‘
EXPORTING
it_list_commentary = i_list_comments
i_logo = ‘ENJOYSAP_LOGO‘
i_end_of_list_grid = ‘X‘.
ENDFORM. "alv_end_of_list
*&---------------------------------------------------------------------*
*& Form alv_end_of_page
*&---------------------------------------------------------------------*
FORM alv_end_of_page.
* CLEAR: i_list_comments[].
* w_list_comments-typ = ‘A‘. "H=Header, S=Selection, A=Action
* w_list_comments-key = ‘‘.
* w_list_comments-info = ‘END OF PAGE‘.
* APPEND w_list_comments TO i_list_comments.
*
* CALL FUNCTION ‘REUSE_ALV_COMMENTARY_WRITE‘
* EXPORTING
* it_list_commentary = i_list_comments
* i_logo = ‘ENJOYSAP_LOGO‘
* i_end_of_list_grid = ‘X‘.
ENDFORM. "alv_end_of_pageFORM layout_build .w_repid = sy-repid. "当前程式i_layout-zebra = ‘X‘. "斑马条纹间隔i_layout-colwidth_optimize = ‘X‘. "优化列宽* i_layout-no_vline = ‘X‘. "用来设置是否有列间隔线i_layout-detail_initial_lines = ‘X‘.i_layout-detail_titlebar = ‘详细内容‘. "设置弹出窗口的标题栏i_layout-detail_popup = ‘X‘. "是否弹出详细信息窗口i_layout-f2code = ‘&ETA‘. "设置触发弹出详细窗口的功能码,这里是双击* i_layout-info_fieldname = ‘COLOR‘ ."设置颜色属性,设置ALV输出报表每一行的颜色,"其参数为输出内表的栏位名称,要注意的是,使用
"该属性需要同时在内表中定义一个与该参数所定义
"字段相同的栏位,然后往该栏位位填值. "先介绍ALV里色码.就是颜色编码,4位CHAR型.
Cxyz---
Color X:1-7
Y:1/0: 强化 开/关
Z:1/0: 相反 开/关
其中C是固定的第一位,第二位代表是颜色编码(1到7),第三位是加强的设置,第四位是相反,个人理解,在强化关闭的情况下,相反的作用是背景和字体的变化.
颜色编码:
x 颜色 主要使用在
1 Gray-blue headers
2 Light gray List bodies
3 yellow totals
4 Blue-green Key columns
5 green Positive threshold value
6 red Negative threshold value
7 orange Control levels
ENDFORM. " LAYOUT_BUILD5. Fieldcat设置pos = pos + 1.
PERFORM set_fieldcat USING pos ‘EXCESS‘ ‘Excess‘ ‘C500‘.FORM set_fieldcat USING p_pos p_field_name p_field_text p_color.w_fieldcat-col_pos = p_pos.
w_fieldcat-fieldname = p_field_name.
w_fieldcat-seltext_m = p_field_text.
w_fieldcat-emphasize = p_color. "设置列的颜色,注意:行的颜色会覆盖列的颜色
w_fieldcat-do_sum = ‘X‘. "对列求和w_fieldcat-just = ‘R‘. "对齐试(R, L, C)w_fieldcat-lzero = ‘X‘. "前导0的形式显示* w_fieldcat-no_sign = ‘X‘. "将定义栏位的符号设置为不显示
* w_fieldcat-hotspot = ‘X‘. "设置栏位是否有热点
APPEND w_fieldcat TO i_fieldcat.ENDFORM. " SET_FIELDCAT
6. 输出REUSE_ALV_GRID_DISPLAYREUSE_ALV_LIST_DISPLAY
7. 颜色的显示FORM output_alv .
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = w_repid
i_save = ‘A‘
i_default = ‘X‘
is_layout = i_layout
it_fieldcat = i_alv_fieldcat
it_events = i_eventsit_sort = i_sort[]i_callback_html_top_of_page = ‘DGL_HTML_TOP_OF_PAGE‘ "覆盖 i_events中定义的.
TABLES
t_outtab = it_output[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.ALV中每行颜色显示都是通过LAYOUT控制的, 要实现每行颜色的控制step 1.
在输出内表中新增一个字段, 用于保存颜色的属性.step 2.给内表的颜色字段赋值.step 3.在LAYOUT中指定内表中定义的颜色字段名称.i_layout-info_fieldname = ‘COLOR‘ ."设置颜色属性,设置ALV输出报表每一行的颜色,8. ALV输出值的排序及汇总该属性设置在IT_SORT接口之中.IT_SORT 的属性有如下:SPOS: 排序顺序,多个字段排序时使用.FIELDNAME: 排序字段名称.UP/DOWN: 升序/降序排序.GROUP: 控制中断.SUBTOT: 对指定字段小计输出. 要实现小计, 首先要保证ALV输出内表的合并功能, FIELDCAT 的DO_SUM字段设置为‘X‘.FORM set_sort .w_sort-fieldname = ‘MATKL‘.w_sort-up = ‘X‘.w_sort-group = ‘*‘.w_sort-subtot = ‘X‘.APPEND w_sort TO i_sort.ENDFORM.这样相同的那列就会合并,如果要避免合并,请在布局中设置"no_merging"为"X" .9. ALV 标题中的表头
表头信息可以通过ALV的接口"I_CALLBACK_HTML_TOP_OF_PAGE"实现.其实表头完全是个HTML文件,所以可参考HTML语法.FORM dgl_html_top_of_page USING r_ddoc TYPE REF TO cl_dd_document.DATA: text TYPE sdydo_text_element.
CALL METHOD r_ddoc->initialize_document. "定义表头文本
DEFINE write_inf.
call method r_ddoc->new_line. "换行
call method r_ddoc->add_icon "输出图标
exporting
sap_icon = &1.
call method r_ddoc->add_text "输出文本
exporting
text = &2.
END-OF-DEFINITION.
CALL METHOD r_ddoc->add_text
EXPORTING
text = ‘Justin Test‘
sap_emphasis = ‘STRONG‘.
write_inf ‘ICON_GREEN_LIGHT‘ ‘: 商品库存‘.
write_inf ‘ICON_RED_LIGHT‘ ‘: 商品无可用库存‘.
ENDFORM.data: m_buff TYPE string.
data: m_p TYPE i.m_buff = ‘<html>‘.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.
m_buff = ‘<center><H2>配件报废品种汇总表</H2></Center>‘.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
CONCATENATE ‘报表日期:‘ S_DATE-LOW ‘ TO ‘ S_DATE-HIGH ‘<BR>‘ into m_buff.
CALL METHOD CL_DD->HTML_INSERT
EXPORTING
CONTENTS = m_buff
CHANGING
POSITION = m_p.
m_buff = ‘</html>‘.
CALL METHOD cl_dd->html_insert
EXPORTING
contents = m_buff
CHANGING
position = m_p.