SAP: ALV GRID 信号灯 例子
Exception 用图形分段显示阶段性数据。ALV 字段由信号灯显示且根据特定字段变更其颜色。这个属性可以使最终用户容易识别紧急性、重要性数据。例如,
在库存管理中将安全库存时设置为绿色,危险时为花色,超过安全库存时为红色。
书中仅写出核心代码, 没有说明插在哪里。
核心代码段:
~~省略~~ DATA: begin of gt_sflight OCCURS 0 . INCLUDE STRUCTURE sflight. DATA: light TYPE c. " 信号灯 DATA: END OF gt_sflight. ~~ 省略~~~ "信号灯" LOOP AT gt_sflight. IF gt_sflight-seatsocc <= 30. gt_sflight-light = '1'. " 1: 红灯 ELSEIF gt_sflight-seatsocc <= 50. gt_sflight-light = '2'. "2: 黄灯 ELSE. gt_sflight-light = '3'. ""3:绿灯 ENDIF. MODIFY gt_sflight. ENDLOOP. " 信号灯 ~~省略~~
~~省略~~ FORM setting_layout CHANGING p_layout type lvc_s_layo. p_layout-cwidth_opt = 'X' . "自动调节长度 p_layout-grid_title = 'Exception Test'. "标题栏" p_layout-sel_mode = 'D'. " 选择模式:可选择多行" p_layout-zebra = 'X'. "指定条纹(STRIPE)属性" p_layout-excp_fname = 'LIGHT'. " 信号灯 字段 ENDFORM. " setting_layout. ~~ 省略 ~~
摸索着,摸索着,做出了效果。 将完整代码 显出如下:
*&---------------------------------------------------------------------* *& Report Z15_20 *& 可执行程序 *&---------------------------------------------------------------------* *& *& 创建 ALV GRID 容器 *& 注意每个程序都需要激活方可执行 *& 实现刷新按钮的 刷新事件。 *& 实现使用字段目录 *& 使用FIELD SYMBOL 功能设置字段显示背景颜色 *& DATA: LIGHT TYPE C. 信号灯。 *&---------------------------------------------------------------------* REPORT Z15_20. TYPE-POOLS: icon. "" 声明图标 *& 内表结构类型 :开始 TYPES: BEGIN OF t_str. INCLUDE STRUCTURE sflight. TYPES: company type c LENGTH 6. TYPES: END OF t_str. *& 内表结构类型 :结束 DATA: CON1_REF TYPE REF TO CL_GUI_CUSTOM_CONTAINER. " 定义容器, 参照 自定义控件"其中自定义控件命名为CON1 DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID. "定义ALV变量, *& 参照 CL_GUI_ALV_GRID类" DATA: GS_VARIANT like DISVARIANT, "" 列表变量 gs_cs_variant like disvariant. *& DATA: GT_SFLIGHT TYPE TABLE OF SFLIGHT. " 要显示在屏幕上的内表 DATA: gs_layout type lvc_s_layo . DATA: gt_toolbar TYPE ui_functions . DATA: gt_sort TYPE lvc_t_sort. " 排序 DATA: gt_fieldcat type lvc_t_fcat. " 声明字段目录变量 *DATA: gt_sflight TYPE TABLE OF T_STR. *DATA: gt_sflight like sflight occurs 0 WITH HEADER LINE. DATA: begin of gt_sflight OCCURS 0 . INCLUDE STRUCTURE sflight. DATA: light TYPE c. " 信号灯 DATA: END OF gt_sflight. TYPES: BEGIN OF TY_SFLIGHT, CARRID TYPE SFLIGHT-CARRID, CONNID TYPE SFLIGHT-CONNID, FLDATE TYPE SFLIGHT-FLDATE, PRICE TYPE SFLIGHT-PRICE, CURRENCY TYPE SFLIGHT-CURRENCY, PLANETYPE TYPE SFLIGHT-PLANETYPE, SEATSMAX TYPE SFLIGHT-SEATSMAX, SEATSOCC TYPE SFLIGHT-SEATSOCC, PAYMENTSUM TYPE SFLIGHT-PAYMENTSUM, SEATSMAX_B TYPE SFLIGHT-SEATSMAX_B, SEATSOCC_B TYPE SFLIGHT-SEATSOCC_B, SEATSMAX_F TYPE SFLIGHT-SEATSMAX_F, SEATSOCC_F TYPE SFLIGHT-SEATSOCC_F, END OF TY_SFLIGHT. PARAMETERS: p_var like disvariant-variant. " 参数变量 *& 设置GRID布局 PERFORM setting_layout CHANGING gs_layout . *& 根据用户显示或隐藏布局按钮 PERFORM setting_toolbar. *& 排序 PERFORM setting_sort. PERFORM getting_catalog. *& 调用设置字段目录 setting_catalog PERFORM setting_catalog. *& 显示检索帮助 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_var. *& gs_variant-report = sy-repid. CALL FUNCTION 'REUSE_ALV_VARIANT_F4' EXPORTING IS_VARIANT = gs_variant * I_TABNAME_HEADER = * I_TABNAME_ITEM = * IT_DEFAULT_FIELDCAT = I_SAVE = 'A' * I_DISPLAY_VIA_GRID = ' ' IMPORTING * E_EXIT = ES_VARIANT = gs_variant EXCEPTIONS NOT_FOUND = 1 PROGRAM_ERROR = 2 OTHERS = 3. IF SY-SUBRC <> 0. * Implement suitable error handling here p_var = gs_variant-variant . ENDIF. FORM setting_layout CHANGING p_layout type lvc_s_layo. p_layout-cwidth_opt = 'X' . "自动调节长度 p_layout-grid_title = 'Exception Test'. "标题栏" p_layout-sel_mode = 'D'. " 选择模式:可选择多行" p_layout-zebra = 'X'. "指定条纹(STRIPE)属性" p_layout-excp_fname = 'LIGHT'. " 信号灯 字段 ENDFORM. " setting_layout. *& 根据用户显示或隐藏布局按钮 FORM setting_toolbar. DATA : l_exclude TYPE UI_FUNC. l_exclude = cl_gui_alv_grid=>mc_fc_save_variant. APPEND l_exclude TO gt_toolbar. l_exclude = cl_gui_alv_grid=>mc_fc_maintain_variant. APPEND l_exclude TO gt_toolbar. ENDFORM. " SETTING_TOOLBAR *& 排序 子程序 FORM setting_sort. DATA: ls_sort TYPE lvc_s_sort. ls_sort-spos = '1'. " 排序顺序 ,整形数字 ls_sort-fieldname = 'CARRID'. " 排序字段名称 ls_sort-up = 'X' . " 升序 或 LS_SORT-DOWN = 'X' "降序 ls_sort-subtot = 'X' . " 显示合计 APPEND ls_sort to gt_sort . ENDFORM. " setting_sort. *& 获取字段目录的子程序 FORM getting_catalog. DATA: lt_fieldcat type kkblo_t_fieldcat. *& 调用函数 (函数组:SKBH) CALL FUNCTION 'K_KKB_FIELDCAT_MERGE' EXPORTING I_CALLBACK_PROGRAM = SY-REPID I_TABNAME = 'GT_SFLIGHT' * I_STRUCNAME = I_INCLNAME = SY-REPID * I_BYPASSING_BUFFER = * I_BUFFER_ACTIVE = CHANGING CT_FIELDCAT = LT_FIELDCAT[] * EXCEPTIONS * INCONSISTENT_INTERFACE = 1 * OTHERS = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. IF SY-SUBRC EQ 0. "调用函数, 函数组:SLVC" CALL FUNCTION 'LVC_TRANSFER_FROM_KKBLO' EXPORTING * I_TECH_COMPLETE = * I_STRUCTURE_NAME = IT_FIELDCAT_KKBLO = lt_fieldcat[] * IT_SORT_KKBLO = * IT_FILTER_KKBLO = * IT_SPECIAL_GROUPS_KKBLO = * IT_FILTERED_ENTRIES_KKBLO = * IT_GROUPLEVELS_KKBLO = * IS_SUBTOT_OPTIONS_KKBLO = * IS_LAYOUT_KKBLO = * IS_REPREP_ID_KKBLO = * I_CALLBACK_PROGRAM_KKBLO = * IT_ADD_FIELDCAT = * IT_EXCLUDING_KKBLO = * IT_EXCEPT_QINFO_KKBLO = IMPORTING ET_FIELDCAT_LVC = gt_fieldcat[] * ET_SORT_LVC = * ET_FILTER_LVC = * ET_SPECIAL_GROUPS_LVC = * ET_FILTER_INDEX_LVC = * ET_GROUPLEVELS_LVC = * ES_TOTAL_OPTIONS_LVC = * ES_LAYOUT_LVC = * ES_VARIANT_LVC = * E_VARIANT_SAVE_LVC = * ES_PRINT_INFO_LVC = * ES_REPREP_LVC = * E_REPREP_ACTIVE_LVC = * ET_EXCLUDING_LVC = * ET_EXCEPT_QINFO_LVC = * TABLES * IT_DATA = * EXCEPTIONS * IT_DATA_MISSING = 1 * OTHERS = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDIF. ENDFORM ." getting_catalog. " *& 设置字段目录的子程序 FORM setting_catalog. DATA: ls_fieldcat type lvc_s_fcat. " 声明 字段目录变更 FIELD-SYMBOLS: <ls_fcat> TYPE lvc_s_fcat. * ls_fieldcat-fieldname = 'COMPANY'. * ls_fieldcat-coltext = 'Company Info'. * ls_fieldcat-just = 'C'. * ls_fieldcat-key = 'X'. * ls_fieldcat-outputlen = '6'. *ls_fieldcat-no_out = 'X'. " 不在ALV GRID *中显示,注意在第一列设置不显示,而第二列以后的列需要显示,则需要在第二列 *&说明显示字段,否则后面字段不显示。 * APPEND ls_fieldcat to gt_fieldcat. * * ls_fieldcat-fieldname = 'CARRID'. "" 字段名称 * ls_fieldcat-coltext = 'Carrid ID'. "" 字段显示名称 ,承运方 * ls_fieldcat-just = 'L'. "" 居左 * ls_fieldcat-KEY = 'X'. "" 主键标记 * ls_fieldcat-outputlen = '2'. "" 输出宽度 * ls_fieldcat-no_out = ''. " 显示字段" * APPEND ls_fieldcat TO gt_fieldcat . * * ls_fieldcat-fieldname = 'CONNID'. * ls_fieldcat-coltext = 'Flight Number'. ""航班号 (连接号) * ls_fieldcat-just = 'C'. "" 居中 * ls_fieldcat-KEY = 'X'. * ls_fieldcat-outputlen = '4'. * APPEND ls_fieldcat TO gt_fieldcat . * * ls_fieldcat-fieldname = 'PRICE'. * ls_fieldcat-coltext = 'Airfare'."" 文本说明(即:字段说明) * ls_fieldcat-just = 'R'. " 居右 * ls_fieldcat-KEY = ''. * ls_fieldcat-outputlen = '15'. * APPEND ls_fieldcat TO gt_fieldcat . *& 循环内表所有字段 * LOOP AT gt_fieldcat INTO ls_fieldcat. * if ls_fieldcat-fieldname = 'PRICE'. * ls_fieldcat-coltext = 'AIR PRICE'. * ls_fieldcat-just = 'C'. * ls_fieldcat-emphasize = 'X'." 设置单元格背景色 * MODIFY gt_fieldcat FROM ls_fieldcat. * ENDIF. * ENDLOOP. *& 在ALV GRID中的前四个字段是主键,颜色指定为蓝色且固定了下面主键对应的 *& 滚动条。 LOOP AT gt_fieldcat ASSIGNING <ls_fcat>. if <ls_fcat>-fieldname EQ 'PRICE'. <ls_fcat>-key = 'X'. " 将单价字段 设置为主键 ENDIF. IF <ls_fcat>-fieldname EQ 'FLDATE'. <ls_fcat>-edit_mask = '____/__/__' . "设置日期格式为: YYYY/MM/DD ENDIF. IF <ls_fcat>-fieldname EQ 'CURRENCY'. <ls_fcat>-fix_column = 'X'. "将 币种 字段设置为固定列 <ls_fcat>-just = 'R'. " 设置对齐方式,R:右对齐,L:左对齐,C:中间对齐。 ENDIF. endloop. ENDFORM. " setting_catalog. *& *CLASS lcl_dragdrop DEFINITION. * PUBLIC SECTION. * DATA: wa type ty_sflight, * index type i. " index of line to be move. *ENDCLASS. " LCL_dragdrop DEFINITION *& CLASS lcl_event_receiver DEFINITION. PUBLIC SECTION. " 声明双击事件方法 METHODS: handle_double_click FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID IMPORTING e_row e_column. " 声明 工具栏事件方法 METHODS: handle_toolbar FOR EVENT toolbar OF CL_GUI_ALV_GRID IMPORTING e_object e_interactive . ""&声明工具栏的刷新按钮事件方法 METHODS: handle_command FOR EVENT user_command OF CL_GUI_ALV_GRID IMPORTING e_ucomm. ""& 声明 拖放方法(Drag & Drop) METHODS: handle_alv_drag FOR EVENT ondrag OF CL_GUI_ALV_GRID IMPORTING e_row e_column e_dragdropobj, handle_alv_drop FOR EVENT ondrop OF CL_GUI_ALV_GRID IMPORTING e_row e_column e_dragdropobj. PRIVATE SECTION. " ENDCLASS. CLASS lcl_event_receiver IMPLEMENTATION. METHOD handle_double_click. "双击事件方法的实现" LEAVE TO SCREEN 0 . ENDMETHOD. " 工具栏事件方法的实现 METHOD handle_toolbar . DATA: ls_toolbar TYPE stb_button. CLEAR ls_toolbar. ls_toolbar-butn_type = 3. APPEND ls_toolbar TO e_object->mt_toolbar. CLEAR ls_toolbar. ls_toolbar-function = 'RESH'. ls_toolbar-icon = icon_refresh. ls_toolbar-quickinfo = 'Refresh'. ls_toolbar-text = ''. ls_toolbar-disabled = ''. APPEND ls_toolbar TO e_object->mt_toolbar. " 增加一个 刷新按钮 ENDMETHOD. "" handle_toolbar ” "& 工具栏刷新按钮的刷新方法的实现 METHOD handle_command. DATA: l_scroll type lvc_s_stbl. CASE e_ucomm. WHEN 'RESH'. SELECT * FROM SFLIGHT INTO TABLE gt_sflight UP TO 40 ROWS . "" 刷新时全表查询 l_scroll-row = 'X'. l_scroll-col = 'X'. CALL METHOD g_grid->refresh_table_display EXPORTING i_soft_refresh = '' is_stable = l_scroll. ENDCASE. ENDMETHOD. " handle_command. METHOD handle_alv_drag. * DATA: l_dragdrop TYPE REF TO lcl_dragdrop. ENDMETHOD. " handle_alv_drag. METHOD handle_alv_drop. * DATA: l_dragdrop TYPE REF TO lcl_dragdrop, * l_drop_index type i, * l_scroll type lvc_s_stbl. " 刷新 ALV GRID 组件 * l_scroll-row = 'X'. * l_scroll-col = 'X'. * CATCH SYSTEM-EXCEPTIONS move_cast_error = 1. * l_dragdrop ?= e_dragdropobj->object. * CALL METHOD g_grid->refresh_table_display * EXPORTING * i_soft_refresh = 'X' * is_stable = l_scroll. * ENDCATCH. IF sy-subrc <> 0. " if anything went wrong aborting the drap and drop operation: CALL METHOD e_dragdropobj->abort. ENDIF. ENDMETHOD. " handle_alv_drop ENDCLASS. " LCL_event_receiver. DATA: event_receiver type ref to lcl_event_receiver . *& 查询数据,然后调用屏幕100. START-OF-SELECTION. SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT UP TO 50 ROWS. "信号灯" LOOP AT gt_sflight. IF gt_sflight-seatsocc <= 30. gt_sflight-light = '1'. " 1: 红灯 ELSEIF gt_sflight-seatsocc <= 50. gt_sflight-light = '2'. "2: 黄灯 ELSE. gt_sflight-light = '3'. ""3:绿灯 ENDIF. MODIFY gt_sflight. ENDLOOP. " 信号灯 CALL SCREEN 100. gs_variant-report = sy-repid. gs_variant-username = sy-uname. *& *& 创建 AVL控件 INCLUDE Z15_20_PBO. *& *& PAI MODULE user_command_0100 INPUT. DATA: l_row type i, l_value type c, l_col type i, ls_row type lvc_s_row, ls_col type lvc_s_col, ls_roid type lvc_s_roid. DATA: ls_scroll type lvc_s_stbl. ls_scroll-row = 'X'. ls_scroll-col = 'X'. *& 模式-> 选择:ABAP对象模式 ,(继续)-> 调用方法, *& 在"实例"栏=G_GRID, 类/接口栏= CL_GUI_ALV_GRID, *方法栏= get_current_cell CALL METHOD G_GRID->GET_CURRENT_CELL IMPORTING E_ROW = l_row E_VALUE = l_value E_COL = l_col ES_ROW_ID = ls_row ES_COL_ID = ls_col ES_ROW_NO = ls_roid. *& 重新查询已经显示在ALV的输出表时使用的方法。 CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY EXPORTING IS_STABLE = ls_scroll I_SOFT_REFRESH = 'X' * EXCEPTIONS * FINISHED = 1 * others = 2 . IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. ENDMODULE. " user_command_0100.
包含文件:
*----------------------------------------------------------------------* ***INCLUDE Z15_17_PBO. *----------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Module INIT_CON OUTPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE INIT_CON OUTPUT. *& 1、创建容器对象 IF CON1_REF IS INITIAL. " 如果容器是空的,创建容器对象" CREATE OBJECT CON1_REF EXPORTING CONTAINER_NAME = 'CON1'. *& 2、创建 ALV GRID 控件 对象 CREATE OBJECT G_GRID EXPORTING I_PARENT = CON1_REF. * EXCEPTIONS * error_cnt1_create = 1 * error_cnt1_init = 2 * error_cnt1_link = 3 * error_dp_create = 4 * OTHERS = 5. * if sy-subrc <> 0. * MESSAGE ID sy-msgid TYPE sy-msgty number sy-msgno * with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 . * endif. ENDIF. " CON1_REF" *& #REGION: 注册事件 CREATE OBJECT event_receiver. * 注册事件Handler方法 SET HANDLER event_receiver->handle_double_click FOR g_grid . *& *& 注册刷新按钮事件 方法 SET HANDLER event_receiver->handle_toolbar FOR g_grid. *& 注册工具栏刷新按钮的刷新事件 方法 SET HANDLER event_receiver->handle_command FOR g_grid. *& 注册 拖放事件 方法 SET HANDLER event_receiver->handle_alv_drop for g_grid. SET HANDLER event_receiver->handle_alv_drag for g_grid. *& #ENDREGION: 注册事件 *& 模式-> 选择:ABAP对象模式 ,(继续)-> 调用方法, 在接口栏=G_GRID, *& 类/接口栏= CL_GUI_ALV_GRID, 方法栏= SET_TABLE_FOR_FIRST_DISPLAY *& 3、 ALV GRID显示数据方法 CALL METHOD g_grid->set_table_for_first_display EXPORTING I_STRUCTURE_NAME = 'SFLIGHT' i_save = 'A' is_variant = gs_variant i_default = ' ' is_layout = gs_layout it_toolbar_excluding = gt_toolbar CHANGING It_OUTTAB = GT_SFLIGHT[] " 若不带表头,则直接使用内表即可(即:GT_SFLGITH), *& 定义内表时若带有表头,则调用ALV时需要传递表体。 it_fieldcatalog = gt_fieldcat " 字段目录 it_sort = gt_sort. ENDMODULE. " INIT_CON OUTPUT *CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY * EXPORTING * I_BUFFER_ACTIVE = * I_BYPASSING_BUFFER = * I_CONSISTENCY_CHECK = * I_STRUCTURE_NAME = * IS_VARIANT = * I_SAVE = * I_DEFAULT = 'X' * IS_LAYOUT = * IS_PRINT = * IT_SPECIAL_GROUPS = * IT_TOOLBAR_EXCLUDING = * IT_HYPERLINK = * IT_ALV_GRAPHICS = * IT_EXCEPT_QINFO = * IR_SALV_ADAPTER = * CHANGING * IT_OUTTAB = * IT_FIELDCATALOG = * IT_SORT = * IT_FILTER = * EXCEPTIONS * INVALID_PARAMETER_COMBINATION = 1 * PROGRAM_ERROR = 2 * TOO_MANY_LINES = 3 * others = 4 * . *IF SY-SUBRC <> 0. ** Implement suitable error handling here *ENDIF.
SAP: ALV GRID 信号灯
标签:gt,fieldcat,DATA,GRID,ls,ALV,SAP,TYPE From: https://www.cnblogs.com/samrv/p/18207289