实现的方式有两种,
1.FUNCTION ALV在it_events的参数中,写入对应的事件
2.OO ALV和FUNCTION ALV都可以,注册当前ALV的事件,对应的事件需要创建对象去实现。
下面代码中,实现的是第一种,在it_events中写入事件
FORM frm_fieldcat.
DEFINE fcat.
CLEAR ls_fieldcat.
ls_fieldcat-fieldname = &1. "字段的名字(内表中定义的字段名)
ls_fieldcat-scrtext_l = &2. "字段的文本描述,输出时候显示。长描述
ls_fieldcat-edit = &3. "当前列可编辑
ls_fieldcat-drdn_hndl = &4.
IF &1 = 'BL_DATUM' OR &1 = 'FKRQ1' OR &1 = 'FKRQ2'.
* ls_fieldcat-HOTSPOT = 'X'.
ls_fieldcat-ref_field = 'ERDAT'.
ls_fieldcat-ref_table = 'VBAK'.
ENDIF.
IF &1 = 'BZ'.
ls_fieldcat-dd_outlen = 50.
ENDIF.
APPEND ls_fieldcat TO lt_fieldcat.
END-OF-DEFINITION.
fcat:
'WADAT_IST' '出货日期' '' '',
'HTEXT' '合同号' '' '', "取自交货单抬头文本
'KUNNR' '客户编码' '' '',
'NAME1' '客户名称' '' '',
'VKORG' '销售组织' '' '',
'L_VBELN' '磅码单号' '' '',
'CHJE' '出货金额' '' '',
'NTGEW' '净重量' '' '',
'BL_DATUM' '开票/BL日期' 'X' '',
'KPJE' '开票金额' 'X' '',
'WAERS' '币种' '' '',
'BL_NUM' '发票号/BL号' 'X' '',
'ZTERM' '付款条件' '' '',
'VTEXT' '付款条件描述' '' '',
'FKTJLB' '付款条件类别' 'X' '1',
'ZTAG1' '账期' '' '',
'DQRQ' '到期日期' '' '',
'FKRQ1' '付款日期1' 'X' '',
'FKJE1' '付款金额1' 'X' '',
'FKRQ2' '付款日期2' 'X' '',
'FKJE2' '付款金额2' 'X' '',
'YUE' '余额' '' '',
'SX' '手续费' 'X' '',
'QK' '欠款' 'X' '',
'SFJQHK' '是否结清货款' '' '',
'BZ' '备注' 'X' ''.
ENDFORM.
FORM call_alv.
ls_layout-cwidth_opt = 'X'. "
自动优化列宽
* ls_layout-BOX_FNAME = 'MARK'.
PERFORM creat_dropdown_values.
PERFORM creat_event_exits.
PERFORM frm_fieldcat.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'FRM_STATUS'
i_callback_user_command = 'FRM_COMMAND'
is_layout_lvc = ls_layout
it_fieldcat_lvc = lt_fieldcat
i_default = 'X'
i_save = 'U'
it_events = lt_events
* IT_EVENT_EXIT =
TABLES
t_outtab = lt_item
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM.
*---------------------------------------------------------------------*
*根据物料的所有的语言生成不同的生成下拉列表
*---------------------------------------------------------------------*
FORM creat_dropdown_values.
CLEAR ls_ddval.
ls_ddval-handle = '1'.
ls_ddval-value = '月结'.
APPEND ls_ddval TO lt_ddval.
CLEAR ls_ddval.
ls_ddval-handle = '1'.
ls_ddval-value = '发货'.
APPEND ls_ddval TO lt_ddval.
CLEAR ls_ddval.
ls_ddval-handle = '1'.
ls_ddval-value = '现款现货'.
APPEND ls_ddval TO lt_ddval.
CLEAR ls_ddval.
ls_ddval-handle = '1'.
ls_ddval-value = '开始日期'.
APPEND ls_ddval TO lt_ddval.
ENDFORM. " creat_dropdown_values
*---------------------------------------------------------------------*
* Form creat_event_exits
*---------------------------------------------------------------------*
FORM creat_event_exits .
ls_events-name = 'CALLER_EXIT'.
ls_events-form = 'CALLER_EXIT'.
APPEND ls_events TO lt_events.
"在此处写入DATA_CHANGED 事件,并且写入对应的FORM名称
ls_events-form = 'FRM_DATA_CHANGED'.
ls_events-name = 'DATA_CHANGED'.
APPEND ls_events TO lt_events.
ENDFORM. " creat_event_exits
*---------------------------------------------------------------------*
*设置下拉列表,使Grid和内表能链接上
*---------------------------------------------------------------------*
FORM caller_exit USING ls_data TYPE slis_data_caller_exit.
DATA: l_ref_alv TYPE REF TO cl_gui_alv_grid.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = l_ref_alv.
CALL METHOD l_ref_alv->set_drop_down_table
EXPORTING
it_drop_down = lt_ddval.
"注册回车事件 .
CALL METHOD l_ref_alv->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified "数据发送改变时触发data changed事件
* i_event_id = cl_gui_alv_grid=>mc_evt_enter "同上,不过这个是回车触发
EXCEPTIONS
error = 1
OTHERS = 2.
* CREATE OBJECT gt_event_receiver . "通过创建对象的形式去调用ALV的事件。
* SET HANDLER gt_event_receiver->data_changed FOR l_ref_alv . " 对应的事件,编辑后
** SET HANDLER gt_event_receiver->handle_modify FOR l_ref_alv ."对应的事件,回车
* CALL METHOD L_REF_ALV->CHECK_CHANGED_DATA.
* CALL METHOD L_REF_ALV->REFRESH_TABLE_DISPLAY.
ENDFORM.
"对应的DATA_CHANGED事件的FROM。具体代码写在这里
*&---------------------------------------------------------------------*
*& Form FRM_DATA_CHANGED
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> ER_DATA_CHANGED
*& --> E_ONF4
*& --> E_ONF4_BEFORE
*& --> E_ONF4_AFTER
*& --> E_UCOMM
*&---------------------------------------------------------------------*
FORM frm_data_changed USING p_data_changed TYPE REF TO cl_alv_changed_data_protocol.
* DATA: LT_MOD TYPE LVC_T_MODI. "被修改的单元格的信息
DATA:l_numc TYPE dlydy,
l_ztage TYPE t052-ztag1,
l_datum TYPE sy-datum.
BREAK-POINT.
LOOP AT p_data_changed->mt_mod_cells INTO DATA(ls_mod).
IF ls_mod-fieldname EQ 'FKTJLB'.
READ TABLE lt_item ASSIGNING FIELD-SYMBOL(<fs_item_list>) INDEX ls_mod-row_id.
IF sy-subrc EQ 0.
"写入实际代码
ENDIF.
ENDIF.
ENDLOOP.
"刷新才能看到数据改变的效果
PERFORM frm_refresh_table.
ENDFORM.
FORM frm_refresh_table.
DATA: ls_stbl TYPE lvc_s_stbl.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = l_ref_alv.
ls_stbl-row = 'X'.
ls_stbl-col = 'X'.
"刷新屏幕
CALL METHOD l_ref_alv->refresh_table_display
EXPORTING
is_stable = ls_stbl
* I_SOFT_REFRESH = 'X'
EXCEPTIONS
finished = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
下面的代码是通过创建对象去实现ALV事件。
*创建响应事件的类及类实现
* 生命类及定义方法类处理date_changed_finished 事件
CLASS lcl_event_receiver DEFINITION .
PUBLIC SECTION .
* METHODS handle_modify FOR EVENT data_changed_finished OF cl_gui_alv_grid
* IMPORTING e_modified et_good_cells .
"数据改变事件
METHODS DATA_CHANGED FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
" 声明Toolbar事件方法
"METHODS handle_toolbar
" FOR EVENT toolbar OF cl_gui_alv_grid
" IMPORTING e_object e_interactive.
" 声明USER-COMMAND 事件方法
* METHODS handle_command
* FOR EVENT user_command OF cl_gui_alv_grid
* IMPORTING e_ucomm.
ENDCLASS .
CLASS lcl_event_receiver IMPLEMENTATION .
* 定义 method 方法
METHOD DATA_CHANGED .
PERFORM FRM_DATA_CHANGED USING ER_DATA_CHANGED.
ENDMETHOD .
ENDCLASS .