总结一下func alv中data_changed的两种实现方法
第一种:直接使用事件data_changed方法,这种方法更简单快速一些
①参数it_events中直接注册data_changed事件,如下:
data: lt_events TYPE slis_t_event,
ls_events TYPE slis_alv_event.
CLEAR: ls_events.
ls_events-name = 'DATA_CHANGED'. "事件
ls_events-form = 'FRM_DATA_CHANGED'. "响应事件的FORM
APPEND ls_events TO lt_events.
//响应事件的form
FORM frm_data_changed USING pcl_data TYPE REF TO cl_alv_changed_data_protocol.
DATA: ls_cells TYPE lvc_s_modi.
LOOP AT pcl_data->mt_mod_cells INTO ls_cells.
BREAK-POINT.
ENDLOOP.
" 刷新alv
DATA: lr_grid TYPE REF TO cl_gui_alv_grid,
ls_stbl TYPE lvc_s_stbl.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
ls_stbl-row = 'X'." 基于行稳定刷新
ls_stbl-col = 'X'." 基于列稳定刷新
CALL METHOD lr_grid->refresh_table_display
EXPORTING
is_stable = ls_stbl.
ENDFORM.
②参数i_grid_settings设置字段edt_cll_cb为’X’.
data:ls_setting TYPE lvc_s_glay.
ls_setting-edt_cll_cb = 'X'.
以上即能实现data_changed,主要就是两个参数的变化,如下:
第二种:结合OOalv实现
①声明、实现类和方法,如下:
DATA: go_grid TYPE REF TO cl_gui_alv_grid. "ALV对象
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
DATA: ucomm TYPE sy-ucomm.
METHODS handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed.
METHODS handle_data_changed_finished FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells sender.
PRIVATE SECTION.
ENDCLASS. "lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_data_changed.
BREAK-POINT.
"PERFORM frm_data_changed USING er_data_changed.
ENDMETHOD. "handle_data_changed
METHOD handle_data_changed_finished.
BREAK-POINT.
"PERFORM frm_finished_changed USING e_modified et_good_cells.
ENDMETHOD. "handle_data_changed_finished
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
DATA: event_receiver TYPE REF TO lcl_event_receiver.
②lt_events中注册CALLER_EXIT事件,在响应的form“FRM_EVENTS_CALLER”中,注册data_changed事件,代码如下:
data: lt_events TYPE slis_t_event,
ls_events TYPE slis_alv_event.
"注册CALLER_EXIT事件,在CALLER_EXIT事件中注册其他事件
CLEAR: ls_events,lt_events.
ls_events-name = 'CALLER_EXIT'.
ls_events-form = 'FRM_EVENTS_CALLER'.
APPEND ls_events TO lt_events.
FORM frm_events_caller USING ls_data TYPE slis_data_caller_exit.
IF go_grid IS INITIAL.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = go_grid.
ENDIF.
"如果call ALV未设置参数回调参数I_GRID_SETTINGS,需要注册EDIT事件
"因此这个方法可以代替第一种情况中的步骤②,任选其一即可
CALL METHOD go_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
CREATE OBJECT event_receiver.
SET HANDLER event_receiver->handle_data_changed FOR go_grid.
SET HANDLER event_receiver->handle_data_changed_finished FOR go_grid.
ENDFORM.
以上就是第二种实现方法。