- 使用REUSE_ALV_GRID_DISPLAY_LVC显示ALV时,可以将列、行或者具体的某个单元格设置成可编辑能输入状态.
- 列可编辑只要将Fieldcat中的字段edit设置成‘X',整列就变成能输入的了,LVC_S_FCAT-EDIT = 'X'.
GT_FIELDCAT TYPE LVC_S_FCAT. gW_FIELDCAT LIKE LINE OF GT_FIELDCAT.... GW_FIELDCAT-EDIT = 'X'. APPEND GW_FIELDCAT-EDIT TO GT_FIELDCAT.
- 整个ALV可编辑 ,Layout的字段edit设置成'X'。
gs_layout type LVC_S_LAYO.gs_layout-edit = 'X'.
- 单元格不可编辑 。如果要让单元格变成可输入,需要在alv输出内表中多定义一个保存单元格style的字段(类型:lvc_t_styl ),然后填入style属性(可编辑或者是不可编辑),最后还要在layout-stylefname中指定style的字段名。
- 内表结构中增加一个字段
TYPES:BEGIN OF ty_data,sel TYPE char1,style TYPE lvc_t_styl, "将该字段设置为保存编辑状态的字段END OF ty_data.
- Fieldcat 赋值( MC_STYLE_DISABLED:禁止输入、MC_STYLE_ENABLED:可输入。)
DATA:gw_edit TYPE lvc_s_styl,gt_edit TYPE lvc_t_styl.gw_edit-fieldname = 'SEL'. gw_edit-style = cl_gui_alv_grid=>mc_style_disabled."设置为不可输入 INSERT gwa_edit INTO TABLE git_edit.INSERT LINES OF gt_edit INTO TABLE gw_data-style. "给ALV内表结构赋值
- Layout赋值
DATA gs_layout TYPE lvc_s_layo.gs_layout-stylefname = 'STYLE'. "指定保存编辑状态的字段名
-
显示效果,可以看到已经有些灰色了...点击也是单击不到的
- 完整代码
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,style TYPE lvc_t_styl,END OF ty_data.DATA gt_data TYPE TABLE OF ty_data. DATA gw_data TYPE ty_data.DATA:gw_edit TYPE lvc_s_styl,gt_edit TYPE lvc_t_styl.** Layout 变量定义 DATA gs_layout TYPE lvc_s_layo.** Fieldcat 变量定义 DATA:gt_fieldcat TYPE lvc_t_fcat,gs_fieldcat LIKE LINE OF gt_fieldcat.** FieldCat的宏 DATA gv_pos TYPE i.DEFINE %%add_fieldcat.gv_pos = gv_pos + 1.gs_fieldcat-col_pos = gv_pos.gs_fieldcat-fieldname = &1.gs_fieldcat-scrtext_m = &2.CASE &1.WHEN 'SEL'.gs_fieldcat-checkbox = 'X'. "当做选择框显示gs_fieldcat-edit = 'X'. "该字段可以编辑ENDCASE.APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat. END-OF-DEFINITION.START-OF-SELECTION.PERFORM get_data.PERFORM dis_falv.FORM get_data.gt_data[] = VALUE #(( sel = '' light = '' name = 'TOM' score = '58')( sel = '' light = '' name = 'JEERY' score = '98')( sel = '' light = '' name = 'MASTER' score = '68')).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 = '优秀'.gw_edit-fieldname = 'SEL'.gw_edit-style = cl_gui_alv_grid=>mc_style_disabled."设置为不可输入INSERT gw_edit INTO TABLE gt_edit.INSERT LINES OF gt_edit INTO TABLE gw_data-style.CLEAR gt_edit[].ENDIF.MODIFY gt_data FROM gw_data.ENDLOOP. ENDFORM.FORM dis_falv. ** Fieldcat字段映射处理%%add_fieldcat:'SEL' '选择', "( 前面字段值不可变 后面字段描述可变 每一行都是)'LIGHT' '状态','REMARK' '备注','NAME' '姓名','SCORE' '分数'.** Layout布局处理gs_layout-cwidth_opt = 'X'. "自动优化列宽 (可以使用**在一行代码面前注释掉 看看ALV会有什么效果)gs_layout-zebra = 'X'. "斑马纹输出gs_layout-stylefname = 'STYLE'. "单元格可编辑 ** 调用FunctionALV需要传入的参数:展示数据,FieldCat,Layout,StatusCALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'EXPORTINGi_callback_program = sy-repidi_callback_pf_status_set = 'FRM_SET_STATUS'is_layout_lvc = gs_layoutit_fieldcat_lvc = gt_fieldcatTABLESt_outtab = gt_data[]EXCEPTIONSprogram_error = 1OTHERS = 2. ENDFORM.FORM frm_set_status USING p_extab TYPE slis_t_extab.SET PF-STATUS 'STANDARD'. ENDFORM.