1.数据定义
"OO ALV
DATA: gc_alv TYPE REF TO cl_gui_alv_grid,
gc_container TYPE REF TO cl_gui_container,
gt_fieldcat TYPE lvc_t_fcat,
gs_fieldcat TYPE lvc_s_fcat,
gs_layout TYPE lvc_s_layo,
gs_variant TYPE disvariant,
gt_exclude TYPE ui_functions.
"自动填充全屏
DATA gc_splitter TYPE REF TO cl_gui_splitter_container.
"事件处理类
DATA gcl_handler TYPE REF TO gcl_events_handle.
2.事件处理类
CLASS gcl_events_handle DEFINITION.
PUBLIC SECTION.
"声明方法 来处理按钮事件
METHODS handle_button_click FOR EVENT button_click OF cl_gui_alv_grid
IMPORTING es_col_id
es_row_no.
"声明方法 来处理事件
METHODS handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object.
"用户命令
METHODS handle_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
"数据改变
METHODS handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed
e_onf4
e_onf4_before
e_onf4_after
e_ucomm.
"数据更新
METHODS handle_data_changed_finished FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING
e_modified
et_good_cells .
METHODS handle_hotspot_click FOR EVENT hotspot_click OF
cl_gui_alv_grid
IMPORTING
e_row_id
e_column_id
es_row_no.
ENDCLASS.
CLASS gcl_events_handle IMPLEMENTATION.
METHOD handle_button_click.
PERFORM frm_button_click USING es_col_id es_row_no.
ENDMETHOD.
METHOD handle_toolbar.
"PERFORM frm_handle_toolbar CHANGING e_object.
ENDMETHOD.
METHOD handle_command.
"PERFORM frm_handle_command USING e_ucomm.
ENDMETHOD.
METHOD handle_data_changed.
PERFORM frm_data_changed USING er_data_changed
e_onf4
e_onf4_before
e_onf4_after
e_ucomm.
ENDMETHOD.
METHOD handle_data_changed_finished.
PERFORM frm_data_changed_finished USING e_modified
et_good_cells .
ENDMETHOD.
METHOD handle_hotspot_click.
"PERFORM frm_hotspot_click USING e_row_id e_column_id es_row_no.
ENDMETHOD.
ENDCLASS.
3.新建一个屏幕,如果是弹窗需要选择对话框
弹框OO ALV
PROCESS BEFORE OUTPUT.
MODULE STATUS_1100.
MODULE INIT_ALV_1100.
PROCESS AFTER INPUT.
MODULE USER_COMMAND_1100.
********************PBO**********************
MODULE status_1100 OUTPUT.
SET PF-STATUS 'STATUS_FOR_1100'.
SET TITLEBAR '1100'.
ENDMODULE.
MODULE init_alv_1100 OUTPUT.
IF gc_alv IS INITIAL.
PERFORM create_object_1100.
PERFORM build_events_1100.
PERFORM build_fieldcat_1100.
PERFORM build_layout_1100.
PERFORM build_variant_1100.
PERFORM build_exclude_1100.
"PERFORM build_event_1200.
PERFORM build_alv_1100.
ELSE.
PERFORM frm_refresh_alv_1100.
ENDIF.
ENDMODULE.
********************PAI**********************
MODULE user_command_1100 INPUT.
CALL METHOD gc_alv->check_changed_data.
CASE sy-ucomm.
WHEN 'CANCEL' OR 'EXIT' OR 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'SELECTALL'. "全选
"XXXX
WHEN 'REFRESH'.
PERFORM frm_get_data.
PERFORM frm_refresh_alv_1100.
ENDCASE.
ENDMODULE.
ALV构造
FORM create_object_1100 .
"自动设置全屏
IF gc_splitter IS INITIAL .
CREATE OBJECT gc_splitter
EXPORTING
parent = cl_gui_container=>screen0
rows = 1
columns = 1.
CALL METHOD gc_splitter->get_container
EXPORTING
row = 1
column = 1
RECEIVING
container = gc_container.
ENDIF .
CREATE OBJECT gc_alv
EXPORTING
i_parent = gc_container.
CREATE OBJECT gcl_handler.
ENDFORM.
"设置弹框OO ALV
FORM create_object_1200 .
CREATE OBJECT gc_container1
EXPORTING
" clsid = '1'
container_name = 'GC_CONTAINER_1200'.
CREATE OBJECT gc_alv1
EXPORTING
i_parent = gc_container1.
CREATE OBJECT gcl_handler1.
ENDFORM.
FORM build_events_1100 .
"设置编辑事件
CALL METHOD gc_alv->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
"设置回车事件
CALL METHOD gc_alv->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
SET HANDLER gcl_handler->handle_button_click FOR gc_alv.
SET HANDLER gcl_handler->handle_data_changed FOR gc_alv.
SET HANDLER gcl_handler->handle_data_changed_finished FOR gc_alv.
ENDFORM.
FORM build_fieldcat_1100 .
CLEAR gt_fieldcat.
DATA lv_pos TYPE i.
DEFINE %%append_fieldcat.
CLEAR gs_fieldcat.
lv_pos = lv_pos + 1.
gs_fieldcat-col_pos = lv_pos.
gs_fieldcat-fieldname = &1.
gs_fieldcat-scrtext_m = &2.
gs_fieldcat-outputlen = &3.
CASE &1.
WHEN 'BOX'.
gs_fieldcat-checkbox = 'X'.
gs_fieldcat-edit = 'X'.
WHEN 'BTN'.
"设置成按钮
gs_fieldcat-style = cl_gui_alv_grid=>mc_style_button.
ENDCASE.
APPEND gs_fieldcat TO gt_fieldcat.
END-OF-DEFINITION.
%%append_fieldcat: 'BTN' '按钮' '6',
'BOX' '选中' '5'.
ENDFORM.
FORM build_layout_1100 .
gs_layout-zebra = 'X'.
gs_layout-SEL_MODE = 'A'.
ENDFORM.
FORM build_variant_1100 .
gs_variant-report = sy-repid.
gs_variant-handle = '1'. "两个OO ALV 需要设置不同的额handle 不然事件会出问题
ENDFORM.
FORM build_exclude_1100 .
DATA ls_exclude TYPE ui_func.
ls_exclude = cl_gui_alv_grid=>mc_fc_refresh.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_minimum .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_subtot .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_sum .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_average .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_sum .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_subtot .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_sort_asc.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_sort_dsc .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_find .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_filter .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_print .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_print_prev .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_export .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_graph .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_view .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_detail .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_help .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_info .
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_variant.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_maximum.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_check.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row. "粘贴新行
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row. "添加行
APPEND ls_exclude TO gt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_mb_paste. "插入并重写
APPEND ls_exclude TO gt_exclude.
ENDFORM.
FORM build_alv_1100 .
CALL METHOD gc_alv->set_table_for_first_display
EXPORTING
is_variant = gs_variant
i_save = 'U'
is_layout = gs_layout
it_toolbar_excluding = gt_exclude
CHANGING
it_outtab = gt_result
it_fieldcatalog = gt_fieldcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
ENDFORM.
FORM frm_refresh_alv_1100 .
DATA:ls_stable TYPE lvc_s_stbl.
ls_stable-col = 'X'.
ls_stable-row = 'X'.
CALL METHOD gc_alv->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDFORM.
4.处理事件类实现
FORM frm_button_click USING p_col TYPE lvc_s_col
p_row TYPE lvc_s_roid.
READ TABLE gt_result INTO gs_result INDEX p_row-row_id.
CALL SCREEN 1200 STARTING AT 60 6 ENDING AT 100 18.
ENDFORM.
FORM frm_handle_toolbar CHANGING p_object TYPE REF TO cl_alv_event_toolbar_set.
DATA ls_toolbar TYPE stb_button.
CLEAR p_object->mt_toolbar.
"在处理类里的handle_command
CLEAR ls_toolbar.
ls_toolbar-function = 'INSERT'.
ls_toolbar-text = '插入'.
ls_toolbar-icon = icon_insert_row.
ls_toolbar-quickinfo = '插入'.
INSERT ls_toolbar INTO p_object->mt_toolbar INDEX 1.
CLEAR ls_toolbar.
ls_toolbar-function = 'DELETE'.
ls_toolbar-text = '删除'.
ls_toolbar-icon = icon_delete_row.
ls_toolbar-quickinfo = '删除'.
INSERT ls_toolbar INTO p_object->mt_toolbar INDEX 2.
ENDFORM.
FORM frm_handle_command USING p_ucomm.
DATA: lv_count TYPE i,
ls_result TYPE zmms012.
DATA: lt_rowid TYPE lvc_t_roid,
ls_rowid TYPE lvc_s_roid,
lt_row TYPE lvc_t_row.
CASE p_ucomm.
WHEN 'INSERT'.
WHEN 'DELETE'.
ENDCASE.
ENDFORM.
FORM frm_data_changed USING p_data_changed TYPE REF TO cl_alv_changed_data_protocol
p_onf4 TYPE char01
p_onf4_before TYPE char01
p_onf4_after TYPE char01
p_ucomm TYPE sy-ucomm.
DATA: ls_modi TYPE lvc_s_modi.
DATA: ls_result LIKE LINE OF gt_result.
LOOP AT p_data_changed->mt_mod_cells INTO ls_modi.
CASE ls_modi-fieldname.
WHEN 'SENDM'.
CALL METHOD p_data_changed->modify_cell
EXPORTING
i_row_id = ls_modi-row_id.
i_fieldname = 'fieldName'
i_value = 'value'.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
ENDFORM.
FORM frm_data_changed_finished USING p_e_modified
p_et_good_cells.
DATA:ls_stable TYPE lvc_s_stbl.
ls_stable-col = 'X'.
ls_stable-row = 'X'.
CALL METHOD gc_alv->refresh_table_display
EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 1
OTHERS = 2.
ENDFORM.
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/17993.html