- 有时用户为了在报表编辑时更方便一些,会提出ALV编辑时进行下拉框的赋值
- 实现步骤
- 定义一个存储下拉框的内表
**这两个字段是成组出现的 缺一不可 TYPES:BEGIN OF ty_data, list TYPE int4, "下拉列表组 kslx TYPE c LENGTH 20, "考试类型 END OF ty_data. "*定义存储下拉列表的数据内表 DATA:gt_ddval TYPE lvc_t_drop, "设置下拉框内表的值 gt_ddval[] = VALUE #( ( handle = '1' value = '挂科') ( handle = '1' value = '补考') ( handle = '1' value = '通过') ).
- Fieldcat的参数EDIT和DRDN_HNDL赋值
DEFINE %%add_fieldcat. gs_fieldcat-fieldname = &1. gs_fieldcat-scrtext_m = &2. gs_fieldcat-edit = &3. "当前列可编辑 gs_fieldcat-drdn_field = &4. "下拉列表 APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat. END-OF-DEFINITION. ** Fieldcat字段映射处理 %%add_fieldcat:'SEL' '选择' 'X' '', 'LIGHT' '状态' '' '', 'REMARK' '备注' '' '', 'NAME' '姓名' '' '', 'SCORE' '分数' '' '', 'KSLX' '考试类型' 'X' 'LIST'. "这里面的LIST就是ALV输出内表定义里的list
- 生成下拉列表,通过事件GRID和内表连接在一起
**定义事件内表 gt_events TYPE slis_t_event, gw_events TYPE slis_alv_event. **把使 GRID 和 内表 连接在一起的事件存入事件内表 gw_events-name = 'CALLER_EXIT'. "这里是子例程名字 gw_events-form = 'CALLER_EXIT'. "这里是子例程名字 APPEND gw_events TO gt_events. FORM caller_exit USING ls_data TYPE slis_data_caller_exit. DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING e_grid = l_ref_alv. CALL METHOD l_ref_alv->set_drop_down_table EXPORTING it_drop_down = gt_ddval. ENDFORM.
- Function ALV调用
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING it_events = gt_events TABLES t_outtab = gt_data.
- 完整代码
TYPES:BEGIN OF ty_data, sel TYPE char1, light TYPE iconname, name TYPE char10, score TYPE p LENGTH 2 DECIMALS 1, remark TYPE c LENGTH 20, list TYPE int4, "下拉列表组1 kslx TYPE c LENGTH 20, END OF ty_data. DATA gt_data TYPE TABLE OF ty_data. DATA gw_data TYPE ty_data. ** Layout 变量定义 DATA gs_layout TYPE lvc_s_layo. ** Fieldcat 变量定义 DATA:gt_fieldcat TYPE lvc_t_fcat, gs_fieldcat LIKE LINE OF gt_fieldcat. "*定义存储下拉列表的数据 DATA:gt_ddval TYPE lvc_t_drop, gs_ddval TYPE lvc_s_drop, gt_events TYPE slis_t_event, gw_events TYPE slis_alv_event. DEFINE %%add_fieldcat. gs_fieldcat-fieldname = &1. gs_fieldcat-scrtext_m = &2. gs_fieldcat-edit = &3. "当前列可编辑 gs_fieldcat-drdn_field = &4. "下拉列表 APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat. END-OF-DEFINITION. START-OF-SELECTION. PERFORM get_data. PERFORM dis_falv. FORM get_data. CLEAR:gt_data[],gt_ddval[]. gt_data[] = VALUE #( ( sel = '' light = '' name = 'TOM' score = '58' list = 1 ) ( sel = '' light = '' name = 'JEERY' score = '98' list = 1 ) ( sel = '' light = '' name = 'MASTER' score = '68' list = 1 ) ). "设置下拉框的值 gt_ddval[] = VALUE #( ( handle = '1' value = '挂科') ( handle = '1' value = '补考') ( handle = '1' value = '通过') ). LOOP AT gt_data INTO gw_data. IF gw_data-score < 60. gw_data-light = icon_red_light. gw_data-remark = '不及格'. ELSEIF gw_data-score >= 60 AND gw_data-score < 85. gw_data-light = ICON_yellow_LIGHT. gw_data-remark = '良好'. ELSEIF gw_data-score >= 85. gw_data-light = ICON_green_LIGHT. gw_data-remark = '优秀'. ENDIF. MODIFY gt_data FROM gw_data. ENDLOOP. ENDFORM. FORM dis_falv. ** Fieldcat字段映射处理 %%add_fieldcat:'SEL' '选择' 'X' '', 'LIGHT' '状态' '' '', 'REMARK' '备注' '' '', 'NAME' '姓名' '' '', 'SCORE' '分数' '' '', 'KSLX' '考试类型' 'X' 'LIST'."这里面的LIST就是ALV输出内表定义里的list ** Layout布局处理 gs_layout-cwidth_opt = 'X'. "自动优化列宽 (可以使用**在一行代码面前注释掉 看看ALV会有什么效果) gs_layout-zebra = 'X'. "斑马纹输出 ** 事件处理 gw_events-name = 'CALLER_EXIT'. gw_events-form = 'CALLER_EXIT'. APPEND gw_events TO gt_events. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid is_layout_lvc = gs_layout it_fieldcat_lvc = gt_fieldcat[] it_events = gt_events TABLES t_outtab = gt_data. ENDFORM. FORM caller_exit USING ls_data TYPE slis_data_caller_exit. DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING e_grid = l_ref_alv. CALL METHOD l_ref_alv->set_drop_down_table EXPORTING it_drop_down = gt_ddval. ENDFORM.