展示效果如下:
实现步骤:
一、在内表中有几个下拉框字段就需要设置几个INT类型的字段用于接受下拉框.
二、声明ALV下拉列表框LVC_T_DROP 和LVC_S_DROP
三、在定义ALV字段属性时,需要将drdn_field 字段赋值内表相应的下拉框字段,如需编辑启用edit = 'X'.
四、对下拉列表框进行赋值
五、声明事件触发器,并将下拉框赋值给事件。ALV引用事件触发器。
DEMO源码如下:
DATA : BEGIN OF gs_data, matnr TYPE mara-matnr, mtart TYPE mara-mtart, matkl TYPE mara-matkl, ztools type char4, WERKS TYPE MARc-WERKS, dd_handle TYPE int4, dd_handles TYPE int4, ZFLAG TYPE C, END OF gs_data. DATA:gt_data LIKE TABLE OF gs_data. DATA: gt_field TYPE lvc_t_fcat, gw_field TYPE lvc_s_fcat, gt_ddval TYPE lvc_t_drop, gw_ddval TYPE lvc_s_drop, gt_events TYPE slis_t_event, gw_events TYPE slis_alv_event, gs_layout TYPE lvc_s_layo, g_repid TYPE sy-repid VALUE sy-repid. SELECT-OPTIONS: s_matnr FOR gs_data-matnr . START-OF-SELECTION. SELECT matnr mtart matkl INTO TABLE gt_data UP TO 50 ROWS FROM mara WHERE matnr IN s_matnr. PERFORM creat_fieldcat. PERFORM creat_dropdown_values. PERFORM creat_event_exits. gs_layout-box_FNAME = 'ZFLAG'. gs_layout-zebra = 'X'. gs_layout-cwidth_opt = 'X'."自动优化列宽 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = g_repid it_fieldcat_lvc = gt_field it_events = gt_events is_layout_lvc = gs_layout i_default = 'X' i_save = 'A' TABLES t_outtab = gt_data. FORM creat_fieldcat. CLEAR gw_field. gw_field-fieldname = 'MATNR'. gw_field-scrtext_l = '物料编码'. APPEND gw_field TO gt_field. CLEAR gw_field. gw_field-fieldname = 'mtart'. gw_field-scrtext_l = '物料类型'. APPEND gw_field TO gt_field. CLEAR gw_field. gw_field-fieldname = 'ztools'. gw_field-scrtext_l = '电脑配置'. gw_field-drdn_field = 'DD_HANDLE'. gw_field-edit = 'X'. APPEND gw_field TO gt_field. CLEAR gw_field. gw_field-fieldname = 'WERKS'. gw_field-scrtext_l = '工厂'. gw_field-drdn_field = 'DD_HANDLEs'. gw_field-edit = 'X'. APPEND gw_field TO gt_field. ENDFORM. " creat_fieldcat FORM creat_dropdown_values. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = 'CPU'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = '主板'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = '显卡'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = '内存'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = '硬盘'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = '电源'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 1. gw_ddval-value = '机箱'. APPEND gw_ddval TO gt_ddval. gw_ddval-handle = 1. gw_ddval-value = '显示器'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 2. gw_ddval-value = '1000 '. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 2. gw_ddval-value = '2000'. APPEND gw_ddval TO gt_ddval. CLEAR gw_ddval. gw_ddval-handle = 2. gw_ddval-value = '3000'. APPEND gw_ddval TO gt_ddval. LOOP AT gt_data INTO gs_data. gs_data-dd_handles = 2. gs_data-dd_handle = 1. MODIFY gt_data FROM gs_data. ENDLOOP. ENDFORM. " creat_dropdown_values FORM creat_event_exits . gw_events-name = 'CALLER_EXIT'. "固定值 gw_events-form = 'CALLER_EXIT'. APPEND gw_events TO gt_events. ENDFORM. " creat_event_exits 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. "CALLER_EXIT