1.定义类
*---------------------------------------------------------------------*
* CLASS lcl_grid_event_receiver DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_grid_event_receiver DEFINITION .
PUBLIC SECTION.
METHODS:
* toolbar
* FOR EVENT toolbar OF cl_gui_alv_grid
* IMPORTING e_object e_interactive ,
user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm ,
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed.
PRIVATE SECTION.
* This flag is set if any error occured in one of the
* following methods:
DATA: error_in_data TYPE c.
METHODS: check_bcje
IMPORTING
ps_good_bcje TYPE lvc_s_modi
pr_data_changed TYPE REF TO cl_alv_changed_data_protocol.
ENDCLASS. "lcl_grid_event_receiver DEFINITION
DATA : grid_handler TYPE REF TO lcl_grid_event_receiver.
2.定义方法
*&---------------------------------------------------------------------*
*& Class (Implementation) lcl_grid_event_receiver
*&---------------------------------------------------------------------*
* Text
*----------------------------------------------------------------------*
CLASS lcl_grid_event_receiver IMPLEMENTATION.
**----------------------------------------------------------------------
** Method to handle user commands from toolbar
**----------------------------------------------------------------------
METHOD user_command.
CASE e_ucomm .
WHEN ''.
ENDCASE.
ENDMETHOD. "user_command
METHOD handle_data_changed.
DATA: ls_good TYPE lvc_s_modi.
error_in_data = space.
LOOP AT er_data_changed->mt_good_cells INTO ls_good.
CASE ls_good-fieldname.
WHEN 'BCJE'.
CALL METHOD check_bcje
EXPORTING
ps_good_bcje = ls_good
pr_data_changed = er_data_changed.
ENDCASE.
ENDLOOP.
ENDMETHOD. "handle_data_changed
METHOD check_bcje.
DATA bcje TYPE ekpo-netwr.
CALL METHOD pr_data_changed->get_cell_value
EXPORTING
i_row_id = ps_good_bcje-row_id
i_fieldname = ps_good_bcje-fieldname
IMPORTING
e_value = bcje.
break-point.
IF bcje IS NOT INITIAL.
READ TABLE item into type_item1 INDEX ps_good_bcje-row_id.
* ASSIGN item TO <ls_fieldcat1>.
* <ls_fieldcat1>-bcje = bcje.
type_item1-bcje = bcje.
MODIFY item from type_item1 INDEX ps_good_bcje-row_id.
CLEAR:item,type_item1.
CALL METHOD alv_grid->refresh_table_display .
ENDIF.
ENDMETHOD. "check_planetype
ENDCLASS. "lcl_grid_event_receiver
3.在build_objects里添加回车事件
IF grid_handler IS INITIAL.
CREATE OBJECT grid_handler.
SET HANDLER:
grid_handler->user_command FOR alv_grid,
grid_handler->handle_data_changed FOR alv_grid."加回车事件
ENDIF.
4.在初始化里加上一下语句
CALL METHOD alv_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.
CALL METHOD alv_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
现在可以发现你在单元格里输入某个数据后,按回车或者将鼠标放到其他地方,会自动检测你的输入内容并进行你所要的操作!