前言
前面介绍了Abap ALV的整体可编辑、列可编辑和单元格可编辑,但是有时会有根据行项目某个字段的值来控制其他单元格的可编辑性的需求,其中还涉及到ALV刷新的功能。
实战
单元格数据修改后自动刷新
单元格中的数据被修改后,将ALV单元格中的数据立即刷新到ABAP对应的内表中:
方法一:通过对REUSE_ALV_GRID_DISPLAY函数参数i_grid_settings-edt_cll_cb进行设置:
i_grid_settings-edt_cll_cb = 'X' .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTINGi_grid_settings = i_grid_settings
方法二:通过函数参数I_CALLBACK_USER_COMMAND指定的回调Form的参数slis_selfield进行设置:
FORM user_command USING ucomm LIKE sy-ucommselfield selfield TYPE slis_selfield.
selfield-refresh = 'X'.
CASE ucomm.
WHEN 'UPDATE'.
PERFORM frm_update.
ENDCASE.
ENDFORM.
案例
展示效果
完整代码
*数据定义
DATA:
BEGIN OF gs_spfli .
INCLUDE TYPE spfli.
DATA cellstyles TYPE lvc_t_styl.
DATA
END OF gs_spfli.
DATA: gt_fieldcat TYPE TABLE OF lvc_s_fcat WITH HEADER LINE,
gs_layout TYPE lvc_s_layo.
DATA: gt_data LIKE TABLE OF gs_spfli WITH HEADER LINE.
DATA: gs_style TYPE lvc_s_styl,
gt_style TYPE lvc_t_styl.
DATA: gt_events TYPE slis_t_event. "ALV 事件
START-OF-SELECTION.
PERFORM f_getdata.
PERFORM f_catalog.
PERFORM f_get_event.
PERFORM f_alvshow.
FORM f_getdata .
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data FROM spfli.
*设置条件置灰列
LOOP AT gt_data.
IF gt_data-carrid NE 'AA' .
gs_style-fieldname = 'DISTANCE'.
gs_style-style = cl_gui_alv_grid=>mc_style_disabled. "置灰
APPEND gs_style TO gt_style.
gt_data-cellstyles = gt_style.
MODIFY gt_data.
CLEAR gt_style.
ENDIF.
ENDLOOP.
ENDFORM. " F_GETDATA
FORM f_catalog .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE =
i_structure_name = 'SPFLI'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER =
* I_INTERNAL_TABNAME =
CHANGING
ct_fieldcat = gt_fieldcat[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CLEAR gt_fieldcat.
*"对gt_fieldcat属性的修改
READ TABLE gt_fieldcat INDEX 13.
gt_fieldcat-edit = 'X'.
MODIFY gt_fieldcat INDEX 13.
CLEAR gt_fieldcat.
*"对gt_fieldcat属性的修改
READ TABLE gt_fieldcat INDEX 16.
gt_fieldcat-edit = 'X'.
MODIFY gt_fieldcat INDEX 16.
*对layout设置可编辑信息内表
gs_layout-zebra = 'X'.
* GS_LAYOUT-EDIT = 'X'.
gs_layout-cwidth_opt = 'X'.
gs_layout-stylefname = 'CELLSTYLES'."数据内表中哪列为可编辑信息内表
ENDFORM. " F_CATALOG
FORM f_alvshow .
DATA: ls_grid_settings TYPE lvc_s_glay.
ls_grid_settings-edt_cll_cb = 'X' .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = gs_layout
it_events = gt_events
it_fieldcat_lvc = gt_fieldcat[]
i_grid_settings = ls_grid_settings
TABLES
t_outtab = gt_data[].
ENDFORM. " F_ALVSHOW
FORM f_get_event .
DATA ls_events TYPE LINE OF slis_t_event.
ls_events-name = slis_ev_data_changed. "DATA_CHANGED事件
ls_events-form = 'F_DATA_CHANGED'.
APPEND ls_events TO gt_events.
ENDFORM. " F_GET_EVENT
FORM f_data_changed USING p_data TYPE REF TO cl_alv_changed_data_protocol.
DATA: l_name(20),ls_cells TYPE lvc_s_modi.
FIELD-SYMBOLS <fs_value>.
LOOP AT p_data->mt_mod_cells INTO ls_cells."获取被修改的单元格
CLEAR gt_data.
READ TABLE gt_data INDEX ls_cells-row_id."被修改的单元格所对应输出内表行数据
CONCATENATE 'GT_DATA-' ls_cells-fieldname INTO l_name. "修改之前的数据
ASSIGN (l_name) TO <fs_value>."修改前的值
<fs_value> = ls_cells-value. "修改后的新值
"实际上不需要修改内表中的数据,因为只要在该Form中不弹出E类消息,执行完后会自动更新内表
"MODIFY gt_data INDEX ls_cells-row_id.
IF ls_cells-value > 5.
gs_style-fieldname = 'DISTANCE'.
gs_style-style = cl_gui_alv_grid=>mc_style_enabled. "可编辑
APPEND gs_style TO gt_style.
gt_data-cellstyles = gt_style.
MODIFY gt_data INDEX ls_cells-row_id.
CLEAR gt_style.
ENDIF.
ENDLOOP.
*刷新ALV界面
PERFORM f_alvrefresh.
ENDFORM.
FORM f_alvrefresh .
DATA: ls_stbl TYPE lvc_s_stbl,
lo_grid TYPE REF TO cl_gui_alv_grid.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lo_grid.
"稳定刷新
ls_stbl-row = 'X'." 基于行的稳定刷新
" ls_stbl-col = 'X'." 基于列稳定刷新
IF lo_grid IS NOT INITIAL.
CALL METHOD lo_grid->refresh_table_display
EXPORTING
is_stable = ls_stbl.
ENDIF.
ENDFORM. " F_ALVREFRESH