修改BB 的内容,回车后修改CC 的值
步骤:
1. 注册类为对类CL_GUI_ALV_GRID的事件DATA_CHANGED_FINISHED定义一个方法,创建对象gt_evnet_receiver
2. 调用FM 'REUSE_ALV_GRID_DISPLAY_LVC'时要调用事件 slis_ev_caller_exit_at_start来获取ALV对象并给其注册回车事件及处理方法.
*&---------------------------------------------------------------------*
DATA: gt_event TYPE slis_t_event WITH HEADER LINE.
DATA: ref_grid TYPE REF TO cl_gui_alv_grid.
"声明类及定义方法来处理data_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.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_modify.
* PERFORM refresh.
DATA stbl TYPE lvc_s_stbl.
LOOP AT gt_itab INTO wa_itab.
wa_itab-cc = wa_itab-bb * 2 .
MODIFY gt_itab FROM wa_itab.
ENDLOOP.
* 稳定刷新
stbl-row = 'X'." 基于行的稳定刷新
stbl-col = 'X'." 基于列稳定刷新
CALL METHOD ref_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
DATA gt_event_receiver TYPE REF TO lcl_event_receiver .
*&---------------------------------------------------------------------*
gt_event-name = 'CALLER_EXIT'. "slis_ev_caller_exit_at_start事件 调用来获取ALV对象并注册回车事件
gt_event-FORM = 'FM_BUTTON'.
APPEND gt_event .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
* i_callback_user_command = 'USER_COMMAND'
* i_grid_settings = gs_grid
is_layout_lvc = gs_layo
it_fieldcat_lvc = pt_fcat[]
it_events = gt_event[]
* it_event_exit = gt_event_exit[]
TABLES
t_outtab = pt_tab
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
*&---------------------------------------------------------------------*
FORM fm_button USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* EXPORTING
* IR_SALV_FULLSCREEN_ADAPTER =
IMPORTING
* ET_EXCLUDING =
* E_REPID =
* E_CALLBACK_PROGRAM =
* E_CALLBACK_ROUTINE =
e_grid = ref_grid
* ET_FIELDCAT_LVC =
* ER_TRACE =
* E_FLG_NO_HTML =
* ES_LAYOUT_KKBLO =
* ES_SEL_HIDE =
* ET_EVENT_EXIT =
* ER_FORM_TOL =
* ER_FORM_EOL =
.
* CALL METHOD ref_grid->check_changed_data.
* 设置enter事件
CALL METHOD ref_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.
CREATE OBJECT gt_event_receiver.
SET HANDLER gt_event_receiver->handle_modify FOR ref_grid.
ENDFORM. "FM_BUTTON
.
*&---------------------------------------------------------------------*
***************************************************************************************************************
***************************************************************************************************************
***************************************************************************************************************
完整代码,可执行
***************************************************************************************************************
***************************************************************************************************************
***************************************************************************************************************
*&---------------------------------------------------------------------*
*& Report ZFELIX_ALV
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zfelix_alv.
TYPE-POOLS: slis.
DATA: gs_fcat TYPE lvc_s_fcat,
gt_fcat TYPE lvc_t_fcat,
gs_layo TYPE lvc_s_layo,
gs_grid TYPE lvc_s_glay.
DATA: gt_event TYPE slis_t_event WITH HEADER LINE,
gt_event_exit TYPE slis_t_event_exit WITH HEADER LINE.
DATA: ref_grid TYPE REF TO cl_gui_alv_grid..
DATA: BEGIN OF gt_itab OCCURS 0,
aa TYPE c,
bb TYPE i,
cc TYPE p,
style TYPE lvc_t_styl,
END OF gt_itab.
DATA: wa_itab LIKE gt_itab.
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
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.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_modify.
* PERFORM refresh.
DATA stbl TYPE lvc_s_stbl.
LOOP AT gt_itab INTO wa_itab.
wa_itab-cc = wa_itab-bb * 2 .
MODIFY gt_itab FROM wa_itab.
ENDLOOP.
* 稳定刷新
stbl-row = 'X'." 基于行的稳定刷新
stbl-col = 'X'." 基于列稳定刷新
CALL METHOD ref_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
DATA gt_event_receiver TYPE REF TO lcl_event_receiver .
START-OF-SELECTION.
gt_itab-aa = 'aa'.
gt_itab-bb = 1.
gt_itab-cc = '1.2'.
APPEND gt_itab.
gt_itab-aa = 'aa'.
gt_itab-bb = 1.
gt_itab-cc = '1.2'.
APPEND gt_itab.
gt_itab-aa = 'aa'.
gt_itab-bb = 1.
gt_itab-cc = '1.2'.
APPEND gt_itab.
PERFORM display_data .
*&---------------------------------------------------------------------*
*& Form display_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_data .
PERFORM set_fcat.
PERFORM set_layout.
* gt_event-form = 'DATA_CHANGED'.
* gt_event-name = slis_ev_data_changed.
* APPEND gt_event.
* gt_event_exit-ucomm = 'ENTR'. "Enter = 'ENTR' & '/00' & ' '
* APPEND gt_event_exit.
* gt_event_exit-ucomm = '/00'.
* APPEND gt_event_exit.
*
* gt_event_exit-ucomm = ' '.
* APPEND gt_event_exit.
*
* gt_event_exit-ucomm = '&DATA_SAVE'.
* APPEND gt_event_exit.
*
* gt_event_exit-ucomm = '&F03'. "Back
* APPEND gt_event_exit.
* gt_event_exit-ucomm = '&F15'.
* APPEND gt_event_exit.
* gt_event_exit-ucomm = '&F12'.
* APPEND gt_event_exit .
PERFORM show_alv TABLES gt_fcat gt_itab[]
USING gs_layo.
ENDFORM. " DISPLAY_DATA
*&---------------------------------------------------------------------*
FORM set_fcat .
PERFORM csalv_set_fcat USING:
" 'Field' 'Text' 'Edit' 'Zero' 'Type' 'Qfieldname' 'Key' 'Hostspot' 'Emphasize' 'Checkbox'
'AA' 'AA' '' '' '' '' '' '' '' '',
'BB' 'BB' 'X' '' '' '' '' '' '' '',
'CC' 'CC' '' '' '' '' '' '' '' ''.
* 'DD' 'DD' 'X' '' '' '' '' 'X' '' 'X',
* 'EE' 'EE' '' '' '' '' '' '' '' 'X' .
ENDFORM. " SET_FCAT
*&---------------------------------------------------------------------*
FORM set_layout .
gs_layo-zebra = 'X'.
* gs_layout-stylefname = 'FIELD_STYLE'.
* gs_layo-CWIDTH_OPT = 'X'.
ENDFORM. " SET_LAYOUT
*&---------------------------------------------------------------------*
FORM csalv_set_fcat USING p_name
p_text
p_edit
p_zero
p_type
p_qfieldname
p_key
p_hotspot
p_emphasize
p_checkbox.
gs_fcat-fieldname = p_name.
* gs_fcat-reptext_l =
* gs_fcat-selddictxt =
gs_fcat-reptext = p_text.
gs_fcat-edit = p_edit.
gs_fcat-no_zero = p_zero.
gs_fcat-datatype = p_type.
gs_fcat-qfieldname = p_qfieldname.
gs_fcat-key = p_key.
gs_fcat-hotspot = p_hotspot.
gs_fcat-emphasize = p_emphasize.
gs_fcat-checkbox = p_checkbox.
APPEND gs_fcat TO gt_fcat.
ENDFORM. "csalv_set_fcat
*&---------------------------------------------------------------------*
FORM show_alv TABLES pt_fcat
pt_tab
USING ps_layout.
* gs_grid-edt_cll_cb = 'X'.
gt_event-name = 'CALLER_EXIT'. "slis_ev_caller_exit_at_start事件
gt_event-FORM = 'FM_BUTTON'.
APPEND gt_event .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
* i_callback_user_command = 'USER_COMMAND'
* i_grid_settings = gs_grid
is_layout_lvc = gs_layo
it_fieldcat_lvc = pt_fcat[]
it_events = gt_event[]
* it_event_exit = gt_event_exit[]
TABLES
t_outtab = pt_tab
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
ENDFORM. "csalv_set_fcat
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_UCOMM text
* -->PS_SELFIELD text
*----------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN '&DATA_SAVE'.
WHEN ' ' .
WHEN OTHERS.
ENDCASE.
ENDFORM. "user_command
*&---------------------------------------------------------------------*
*& Form SET_PF_STATUS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RT_EXTAB text
*----------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab..
* SET PF-STATUS '100'.
ENDFORM. "SET_PF_STATUS
*&---------------------------------------------------------------------*
*& Form fm_button
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->E_GRID text
*----------------------------------------------------------------------*
FORM fm_button USING e_grid TYPE slis_data_caller_exit.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* EXPORTING
* IR_SALV_FULLSCREEN_ADAPTER =
IMPORTING
* ET_EXCLUDING =
* E_REPID =
* E_CALLBACK_PROGRAM =
* E_CALLBACK_ROUTINE =
e_grid = ref_grid
* ET_FIELDCAT_LVC =
* ER_TRACE =
* E_FLG_NO_HTML =
* ES_LAYOUT_KKBLO =
* ES_SEL_HIDE =
* ET_EVENT_EXIT =
* ER_FORM_TOL =
* ER_FORM_EOL =
.
* CALL METHOD ref_grid->check_changed_data.
* 设置enter事件
CALL METHOD ref_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.
CREATE OBJECT gt_event_receiver.
SET HANDLER gt_event_receiver->handle_modify FOR ref_grid.
ENDFORM. "FM_BUTTON