自用模板,可能存在问题
一、主程序
*&---------------------------------------------------------------------*
*& Report ZVIA_OO_ALV
*&---------------------------------------------------------------------*
REPORT ZVIA_OO_ALV.
INCLUDE ZVIA_OO_ALV_TOP1."OO ALV定义相关 (可能需要修改)
INCLUDE ZVIA_OO_ALV_TOP2."程序数据定义相关 (需要修改)
INCLUDE ZVIA_OO_ALV_S01."选择屏幕,获取用户输入 (需要修改)
INCLUDE ZVIA_OO_ALV_F01."取数逻辑等子例程 (需要修改)
INCLUDE ZVIA_OO_ALV_F02."OO ALV 相关 (需要修改)
START-OF-SELECTION.
PERFORM FRM_GET_DATA.
END-OF-SELECTION.
CALL SCREEN 2000.
二、OO ALV定义相关
*&---------------------------------------------------------------------*
*& 包含 ZVIA_OO_ALV_TOP1
*&---------------------------------------------------------------------*
DATA:
gv_ucomm TYPE sy-ucomm,
ok_code TYPE sy-ucomm.
DATA:
gv_col_pos LIKE sy-cucol,
g_wa_allfields TYPE lvc_s_fcat,
g_grid_alv TYPE REF TO cl_gui_alv_grid, "列表
g_container_alv TYPE scrfname VALUE 'GC_2000', "容器 (可能需要修改)
g_custom_container_alv TYPE REF TO cl_gui_custom_container,
gw_layout_alv TYPE lvc_s_layo,
gt_fieldcat_alv TYPE lvc_t_fcat,
gw_fieldcat_alv TYPE lvc_s_fcat,
gw_exclude_alv TYPE ui_func,
gt_exclude_alv TYPE TABLE OF ui_func.
*OO ALV Display*
DATA:
go_alv TYPE REF TO cl_gui_alv_grid."声明ALV对象
需要创建屏幕2000
三、程序数据相关
*&---------------------------------------------------------------------*
*& 包含 ZVIA_OO_ALV_TOP2
*&---------------------------------------------------------------------*
*根据报表数据结构来定义内表和结构体*
DATA:
git_stu TYPE STANDARD TABLE OF zvia_stu,"内表
gw_stu TYPE zvia_stu."结构体
TABLES:
zvia_stu."使用到的表
四、选择屏幕
确认要使用到的用户选择字段
*&---------------------------------------------------------------------*
*& 包含 ZVIA_OO_ALV_S01
*&---------------------------------------------------------------------*
SELECT-OPTIONS:
s_id FOR zvia_stu-stuid.
五、 取数逻辑等子例程
写自己的取数逻辑,这里写的比较简单
*&---------------------------------------------------------------------*
*& 包含 ZVIA_OO_ALV_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& 取数逻辑
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_get_data .
SELECT *
from ZVIA_STU
INTO TABLE git_stu
WHERE stuid in s_id.
ENDFORM.
六、OO ALV 显示等相关
*&---------------------------------------------------------------------*
*& 包含 ZVIA_OO_ALV_F02
*&---------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_top_of_page
FOR EVENT top_of_page OF cl_gui_alv_grid
IMPORTING e_dyndoc_id,
handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column es_row_no,
handle_toolbar_alv
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
handle_user_command_alv
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm,
handle_onf4_help
FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname
es_row_no
"ER_EVENT_DATE,
er_event_data,
"HANDLE_DATE_CHANGED_alv
HANDLE_DATA_CHANGED_alv
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed.
PRIVATE SECTION.
ENDCLASS.
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_double_click.
MESSAGE 'SHUANG JI' TYPE 'S'.
ENDMETHOD.
METHOD handle_toolbar_alv.
DATA:
ls_toolbar TYPE stb_button.
CLEAR: ls_toolbar.
ls_toolbar-butn_type = 3."分隔符
"APPEND LS_TOOLBAR TO E_ONJECT->MT_TOOLBAR.
APPEND ls_toolbar TO e_object->mt_toolbar.
CLEAR: ls_toolbar.
ENDMETHOD.
METHOD handle_user_command_alv.
ENDMETHOD.
METHOD handle_onf4_help.
er_event_data->m_event_handled = 'X'.
ENDMETHOD.
METHOD handle_data_changed_alv.
PERFORM handle_data_changed USING er_data_changed.
ENDMETHOD.
METHOD handle_top_of_page.
ENDMETHOD.
ENDCLASS.
FORM sub_set_field USING VALUE(p_first)
VALUE(p_col_pos)
VALUE(p_fieldname)
VALUE(p_coltext)
VALUE(p_key)
VALUE(p_edit)
VALUE(p_outputlen)
VALUE(p_f4availabl)
VALUE(p_checkbox).
CLEAR g_wa_allfields.
g_wa_allfields-col_pos = p_col_pos.
g_wa_allfields-fieldname = p_fieldname.
g_wa_allfields-coltext = p_coltext.
g_wa_allfields-key = p_key.
g_wa_allfields-edit = p_edit.
g_wa_allfields-outputlen = p_outputlen.
g_wa_allfields-f4availabl = p_f4availabl.
g_wa_allfields-checkbox = p_checkbox.
IF p_first = '1'.
APPEND g_wa_allfields TO gt_fieldcat_alv.
ENDIF.
ENDFORM.
*&---------------------------------------------------
*&FORM FRM_SET_ALV.行项目-需要修改-ALV报表需要显示哪些列
*&按照自己的需求改这里的列对应的字段和列名--------------
*&---------------------------------------------------
FORM frm_set_alv.
CLEAR:
gv_col_pos,
gt_fieldcat_alv,
gw_fieldcat_alv.
gv_col_pos = gv_col_pos + 1.
PERFORM sub_set_field USING '1' gv_col_pos 'STUID' '学生ID' '' '' '10' '' ''.
gv_col_pos = gv_col_pos + 1.
PERFORM sub_set_field USING '1' gv_col_pos 'STUNAME' '学生姓名' '' '' '10' '' ''.
gv_col_pos = gv_col_pos + 1.
PERFORM sub_set_field USING '1' gv_col_pos 'GENDER' '性别' '' '' '10' '' ''.
gv_col_pos = gv_col_pos + 1.
PERFORM sub_set_field USING '1' gv_col_pos 'AGE' '年龄' '' '' '10' '' ''.
ENDFORM.
*&---------------------------------------------------
*&FORM FRM_PREPARE_LAYOUT
*&ALV 显示相关的参数-比如是否可以编辑,是否可以选择
*&---------------------------------------------------
*&---------------------------------------------------
*&---------------------------------------------------
FORM frm_prepare_layout CHANGING cs_layout TYPE lvc_s_layo.
cs_layout-zebra = 'X'."可选行颜色
cs_layout-smalltitle = 'X'."标题大小
cs_layout-edit_mode = 'X'."编辑模式
cs_layout-cwidth_opt = 'X'."优化列宽度
cs_layout-edit = ''."准备输入
* CS_LAYOUT-NO ROWMWARK = ''."禁用行选择
* CS_LAYOUT-SEL_MODE = 'R'."选择方式
cs_layout-stylefname = 'STYLE'."内部表字段的字段名称
cs_layout-info_fname = 'CLR'."WEB中显示的行数
* CS_LAYOUT-NO_HGRIDLN = 'X'."隐藏水平网格线
ENDFORM.
*&---------------------------------------------------
*&FORM HANDLE_DATA_CHANGED
*&---------------------------------------------------
FORM handle_data_changed USING p_er_data_changed TYPE REF TO cl_alv_changed_data_protocol.
DATA: lw_mod_cell TYPE lvc_s_modi.
DATA: lv_jine TYPE p DECIMALS 4.
DATA:lv_shul TYPE p DECIMALS 4.
IF p_er_data_changed->mt_mod_cells IS NOT INITIAL.
LOOP AT p_er_data_changed->mt_mod_cells INTO lw_mod_cell.
ENDLOOP.
PERFORM sub_alv_refresh.
ENDIF.
ENDFORM.
*&---------------------------------------------------
*&FORM SUB_ALV_REFRESH
*&---------------------------------------------------
FORM sub_alv_refresh.
DATA: lw_stable TYPE lvc_s_stbl."刷新稳定性
lw_stable-row = 'X'.
lw_stable-col = 'X'.
CALL METHOD g_grid_alv->refresh_table_display
EXPORTING is_stable = lw_stable.
ENDFORM.
*&---------------------------------------------------
*&MODULE STATUS_2000 OUTPUT.
*&---------------------------------------------------
*&这里面需要手动创建STATUS/TITLEBAR-------------------
*&---------------------------------------------------
MODULE status_2000 OUTPUT.
SET PF-STATUS 'ZPF'."复制标准菜单栏(双击创建)
SET TITLEBAR 'ZTITLE'."ALV显示的标题行(双击创建)
PERFORM frm_prepare_layout CHANGING gw_layout_alv.
PERFORM frm_2000_alv_show.
ENDMODULE.
*&---------------------------------------------------
*&MOUDLE USER_COMMAND_2000 INPUT
*&---------------------------------------------------
*&用来响应用户的操作----------------------------------
*&---------------------------------------------------
MODULE user_command_2000 INPUT.
gv_ucomm = ok_code.
CASE gv_ucomm.
WHEN '&F03' OR '&F12' OR '&F15'.
LEAVE TO SCREEN 0."返回上一屏幕
WHEN 'TEST'.
MESSAGE 'TEST CLICKED' TYPE 'S'.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------
*&FORM SUB SCREEN 2000
*&---------------------------------------------------
FORM sub_screen_2000.
PERFORM frm_prepare_layout
CHANGING
gw_layout_alv.
ENDFORM.
*&---------------------------------------------------
*&FORM FRM 2000 ALV SCREEN
*&---------------------------------------------------
FORM frm_2000_alv_show.
DATA:
lw_variant TYPE disvariant,
lo_event_handler_alv TYPE REF TO lcl_event_receiver.
lw_variant-report = sy-repid.
PERFORM frm_set_alv.
CREATE OBJECT lo_event_handler_alv.
IF g_grid_alv IS INITIAL.
CREATE OBJECT g_custom_container_alv
EXPORTING
container_name = g_container_alv."带参数的构造函数
CREATE OBJECT g_grid_alv
EXPORTING
i_parent = g_custom_container_alv."带参数的构造函数
CALL METHOD g_grid_alv->set_table_for_first_display
EXPORTING
is_layout = gw_layout_alv
it_toolbar_excluding = gt_exclude_alv
i_save = 'A'
is_variant = lw_variant
CHANGING
it_fieldcatalog = gt_fieldcat_alv
it_outtab = git_stu[].
SET HANDLER lo_event_handler_alv->handle_toolbar_alv FOR g_grid_alv.
CALL METHOD g_grid_alv->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
*用户操作
SET HANDLER lo_event_handler_alv->handle_user_command_alv FOR g_grid_alv.
SET HANDLER lo_event_handler_alv->handle_data_changed_alv FOR g_grid_alv.
*ALV双击
SET HANDLER lo_event_handler_alv->handle_double_click FOR g_grid_alv.
CALL METHOD g_grid_alv->set_toolbar_interactive.
ELSE.
CALL METHOD g_grid_alv->refresh_table_display.
CALL METHOD cl_gui_cfw=>flush.
ENDIF.
ENDFORM.