项目当中经常要求编辑ALV的某列或者是某个字段,有很多博友都介绍过其实现方法,如果照搬可能遇到一些问题,下面具体地描述下:
想让一列可以编辑,我们可以在字段目录中设置EDIT为"X".在可编辑的情况下,ALV会自动多出来几个编辑按钮,删除,新增,插入,复制.如果你不需要它们,可以使用(ABAP-AVL-如何去掉OO方法中的ALV的标准按钮ABAP-AVL-如何去掉OO方法中的ALV的标准按钮)
单元格级别的可编辑和不可编辑,是内表字段,参考表类型"LVC_T_STYL".不过填入的style应该为CL_GUI_ALV_GRID=>MC_STYLE_ENABLED和CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.指定字段名就可以.CL_GUI_ALV_GRID=>MC_STYLE_ENABLED使字段可以编辑,而CL_GUI_ALV_GRID=>MC_STYLE_DISABLED使字段不可以编辑
详细步骤一: 在要显示的alv字段列表中增加一个字段,类型如下
cellstyles TYPE lvc_t_styl,
步骤二:进行layout设置
gs_layout-stylefname = 'CELLSTYLES'.
步骤三:
FORM adjust_edittables TABLES pt_result LIKE gt_result.
DATA ls_result LIKE LINE OF pt_result.
DATA ls_stylerow TYPE lvc_s_styl.
DATA lt_styletab TYPE TABLE OF lvc_s_styl.
CLEAR ls_result.
LOOP AT pt_result INTO ls_result.
IF ls_result EQ gs_result.
CLEAR ls_stylerow.
ls_stylerow-fieldname = ''.
ls_stylerow-style = cl_gui_alv_grid=>mc_style_enabled.
* APPEND ls_stylerow TO ls_result-cellstyles .
APPEND ls_stylerow TO lt_styletab .
CLEAR ls_stylerow.
ls_stylerow-fieldname = ''.
ls_stylerow-style = cl_gui_alv_grid=>mc_style_enabled.
APPEND ls_stylerow TO lt_styletab .
* APPEND ls_stylerow TO ls_result-cellstyles.
ENDIF.
ls_result-cellstyles[] = lt_styletab[].
* INSERT lines of lt_styletab INTO ls_result-cellstyles .
MODIFY pt_result FROM ls_result .
ENDLOOP.
ENDFORM. " adjust_edittables
注意事项:好多朋友贴的是下面这段代码,可能存在着一些问题:
A:lvc_t_styl是一个排序表,append的时候可能会出错
B:INSERT LINES OF lt_styletab INTO ls_listrow-cellstyles.可能也存在错误
FORM adjust_edittables USING pt_list LIKE gt_list[].
DATA ls_listrow LIKE LINE OF pt_list.
DATA ls_stylerow TYPE lvc_s_styl.
DATA lt_styletab TYPE lvc_t_styl.
LOOP AT pt_list INTO ls_listrow.
IF ls_listrow-carrid = 'XY'.
Ls_stylerow-fieldname = 'SEATSMAX'.
Ls_stylerow-style = cl_alv_grid=>mc_style_disabled.
APPEND ls_stylerow TO lt_styletab.
ENDIF.
IF ls_listrow-connid = '02'.
Ls_stylerow-fieldname = 'PLANETYPE'
Ls_stylerow-style = cl_alv_grid=>mc_style_enabled.
APPEND ls_Pstylerow TO lt_styletab.
ENDIF.
INSERT LINES OF lt_styletab INTO ls_listrow-cellstyles.
MODIFY pt_list FROM ls_listrow.
ENDLOOP.
ENDFORM.
如何将某列设置为button:
详细步骤一: 在要显示的alv字段列表中增加一个字段,类型如下
cellstyles TYPE lvc_t_styl,
步骤二:进行layout设置
gs_layout-stylefname = 'CELLSTYLES'.
步骤三:
FORM set_column_button .
DATA ls_style TYPE lvc_s_styl.
DATA lw_result LIKE LINE OF gt_result.
DATA: l_tabix LIKE sy-tabix.
LOOP AT gt_result INTO lw_result.
l_tabix = sy-tabix.
* READ TABLE gt_result INDEX 3 into lw_result.
ls_style-fieldname = 'MATNR'.
ls_style-style = cl_gui_alv_grid=>mc_style_button.
APPEND ls_style TO lw_result-cellstyles.
MODIFY gt_result FROM lw_result INDEX l_tabix .
CLEAR lw_result.
CLEAR ls_style.
ENDLOOP.
ENDFORM.
设置某个单元格的f4:
步骤一:(可以放在设置fieldcat的form里)
DATA: gs_f4 TYPE lvc_s_f4.
gs_f4-fieldname = 'MAKTX'.
gs_f4-register = 'X'.
gs_f4-getbefore = 'X'.
gs_f4-chngeafter = 'X'.
INSERT gs_f4 INTO TABLE gt_f4.
步骤二:(写在class ---》
lcl_event_receiver里 )
handle_on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname
es_row_no
er_event_data
et_bad_cells.
步骤三:
SET HANDLER grf_event_receiver->handle_on_f4 FOR grf_grid."向ALV创建F4事件
CALL METHOD grf_grid->register_f4_for_fields
EXPORTING
it_f4 = gt_f4.
呵呵 有些乱!!!!!
阅读(6640) | 评论(0) | 转发(1) |