导语:function alv相对于ooalv没有那么灵活,但是该有的东西也都有,只不过实现方式不同,下面说一下function alv回车事件如何实现,回车事件一半伴随着ALV编辑,以及编辑后优化列宽,以前都有分享过。
一、定义
"ALV定义
DATA: gt_fieldcat TYPE lvc_t_fcat .
DATA: gs_fieldcat TYPE lvc_s_fcat .
DATA: gs_layout TYPE lvc_s_layo .
DATA: gt_events TYPE slis_t_event. "增加事件使用
DATA: gs_events TYPE slis_alv_event.
DATA:stbl TYPE lvc_s_stbl.
DATA:g_grid TYPE REF TO cl_gui_alv_grid.
二、定义类
回车事件都是为ALV可编辑服务的,那么ALV编辑后写入内表就离不开changed finished事件,所以要定义一下类
*----------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* 类定义
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION."类定义
PUBLIC SECTION.
METHODS:
handle_data_changed_finished FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells.
ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
* 类实现
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION."类实现
METHOD handle_data_changed_finished.
DATA : ls_layout TYPE lvc_s_layo."一定要重新定义一个layout
PERFORM handle_data_changed_finished USING e_modified et_good_cells.."具体处理数据事件
IF e_modified = 'X'.
CALL METHOD g_grid->get_frontend_layout
IMPORTING
es_layout = ls_layout.
"编辑完自动优化列宽
ls_layout-cwidth_opt = 'X'.
CALL METHOD g_grid->set_frontend_layout
EXPORTING
is_layout = ls_layout.
"刷新ALV
stbl-row = 'X'." 基于行的稳定刷新
stbl-col = 'X'." 基于列稳定刷新
CALL METHOD g_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDIF.
ENDMETHOD.
ENDCLASS. "lcl_event_handler IMPLEMENTATION
三、回车事件准备
我一般喜欢在填充layout的时候把ALV需要的参数都准备好,所以演示就放在LAYOUT里了。
FORM frm_fill_lauout .
"定义列自动宽度优化
gs_layout-cwidth_opt = 'X'.
"布局
gs_layout-sel_mode = 'A'.
"定义条纹显示
gs_layout-zebra = 'X'.
IF p_crt_l = 'X' AND p_bwart = 'XXX'.
**增加ALV事件
gs_events-name = 'CALLER_EXIT'. "事件名称
gs_events-form = 'FRM_REGISTER_EVENTS'. "对应的FORM
APPEND gs_events TO gt_events.
"F4 事件 如果有多个事件,需要给name不同的名字
* gs_events-name = 'CALLER_EXIT2'.
* gs_events-form = 'FRM_F4'.
* APPEND gs_events TO gt_events.
ENDIF.
ENDFORM.
同时在ALV输出函数,给IT_EVENT传参
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'STATUS'
i_callback_user_command = 'USER_COMMAND'
is_layout_lvc = gs_layout
it_events = gt_events "新增事件
it_fieldcat_lvc = gt_fieldcat
TABLES
t_outtab = gt_out
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE 'E'.
ENDIF.
四、实现回车事件
*&---------------------------------------------------------------------*
*& Form register_events
*&---------------------------------------------------------------------*
* 注册回车事件
*----------------------------------------------------------------------
FORM frm_register_events USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = g_grid.
*
DATA: gr_event_handler TYPE REF TO lcl_event_handler.
CREATE OBJECT gr_event_handler.
*
CALL METHOD g_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.
"调用数据写入
SET HANDLER gr_event_handler->handle_data_changed_finished FOR g_grid.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. "register_events
五、数据写入内表
ALV编辑后写入内表之前有文章分享过,在本篇文章开始有链接,这里为了完整演示,也把代码贴出来。
*&---------------------------------------------------------------------*
*& Form handle_data_changed_finished
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> E_MODIFIED
*& --> ET_GOOD_CELLS
*&---------------------------------------------------------------------*
FORM handle_data_changed_finished USING e_modified TYPE char01
et_good_cells TYPE lvc_t_modi.
DATA : lt_cells TYPE lvc_t_modi.
CLEAR lt_cells.
lt_cells[] = et_good_cells[].
LOOP AT lt_cells INTO DATA(ls_cells).
READ TABLE gt_pp037 ASSIGNING FIELD-SYMBOL(<fs_pp037>) INDEX ls_cells-row_id.
IF sy-subrc = 0 AND ls_cells-fieldname = 'MATNR'.
IF <fs_pp037>-matnr IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = <fs_pp037>-matnr
IMPORTING
output = <fs_pp037>-matnr.
SELECT SINGLE maktx FROM makt INTO <fs_pp037>-maktx WHERE matnr = <fs_pp037>-matnr.
SELECT SINGLE meins FROM mara INTO <fs_pp037>-meins WHERE matnr = <fs_pp037>-matnr.
SELECT SINGLE msehl FROM t006a INTO <fs_pp037>-msehl WHERE msehi = <fs_pp037>-meins AND spras = '1'.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
EXPORTING
input = <fs_pp037>-matnr
IMPORTING
output = <fs_pp037>-matnr.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
六、效果演示
作者:小飞猪猪猪猪猪猪猪–CSDN