ABAP用OO方式实现ALV
report ytest_w_001.
*" 功能A: 基本显示
*" 功能B: 设定layout
*" 功能C: 设定fieldcat
*" 功能D: 增加复选框
*" 功能E: 设定某列可输入
*" 功能F: 设定某列为下拉列表
*" 功能G: 设定屏蔽标准ALV 工具栏的按钮
*" 功能H: 设定增加自定义ALV工具栏的按钮
*" 功能I: 设定响应双击
*" 功能J: 设定响应user command
*" 功能K: 设定TOP_OF_PAGE
*" 功能L: 设定动态修改layout
*" 功能M: 设定动态修改fieldcat
代码诠释:
data: begin of gt_user occurs 0."LIKE TABLE OF usr02." with header line.
include structure usr02.
data: checkbox(1)," 功能D: 增加复选框
color(4).
data: end of gt_user.
data: g_container type ref to cl_gui_custom_container." 功能A: 基本显示
data: g_grid type ref to cl_gui_alv_grid." 功能A: 基本显示
data: save_ok like sy-ucomm,
ok_code like sy-ucomm.
data: gt_fieldcat type lvc_t_fcat, " 功能C: 设定fieldcat
gs_fieldcat type lvc_s_fcat." 功能C: 设定fieldcat
data: ls_layout type lvc_s_layo. " 功能B: 设定layout
data: gt_exclude type ui_functions." 功能G: 设定屏蔽标准ALV 工具栏的按钮
*----------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handler definition.
public section.
methods handle_double for event double_click of cl_gui_alv_grid importing e_row e_column . " 功能I: 设定响应双击
methods handle_toolbar for event toolbar of cl_gui_alv_grid importing e_object. " 功能H: 设定增加自定义ALV工具栏的按钮
methods handle_user_command for event user_command of cl_gui_alv_grid importing e_ucomm. " 功能J: 设定响应user command
methods handle_top for event top_of_page of cl_gui_alv_grid. " 功能K: 设定TOP_OF_PAGE
endclass. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handler implementation.
method handle_double. " 功能I: 设定响应双击
message i000(ih) with e_row-index e_column-fieldname.
endmethod. "handle_double
method handle_toolbar. " 功能H: 设定增加自定义ALV工具栏的按钮
data: ls_toolbar type stb_button.
ls_toolbar-butn_type = '3'.
append ls_toolbar to e_object->mt_toolbar.
clear ls_toolbar.
ls_toolbar-text = 'Click'.
ls_toolbar-function = 'Z1'.
ls_toolbar-icon = icon_calculation.
*QUICKINFO
*BUTN_TYPE
append ls_toolbar to e_object->mt_toolbar.
endmethod. "handle_toolbar
method handle_user_command. " 功能J: 设定响应user command
case e_ucomm.
when 'Z1'.
message i000(ih) with 'Y'.
endcase.
endmethod. "handle_user_command
method handle_top. " 功能K: 设定TOP_OF_PAGE
write:/ 'this is end'.
message i000(ih) with 'hi'.
endmethod.
endclass. "lcl_event_handler IMPLEMENTATION
data: gr_event type ref to lcl_event_handler.
start-of-selection.
perform frm_get_data.
call screen 100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0100 output.
set pf-status '100'.
set titlebar '100'.
if g_container is initial.
create object g_container" 功能A: 基本显示
exporting
* parent =
container_name = 'GC_1' .
create object g_grid" 功能A: 基本显示
exporting
i_parent = g_container
exceptions
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
others = 5 .
gs_fieldcat-fieldname = 'CHECKBOX'. " 功能D: 增加复选框
gs_fieldcat-coltext = 'CHECK'. " 功能D: 增加复选框
gs_fieldcat-checkbox = 'X'. " 功能D: 增加复选框
gs_fieldcat-edit = 'X'. " 功能E: 设定某列可输入
append gs_fieldcat to gt_fieldcat. " 功能D: 增加复选框
call function 'LVC_FIELDCATALOG_MERGE' " 功能C: 设定fieldcat
exporting
* I_BUFFER_ACTIVE =
i_structure_name = 'USR02'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER =
* I_INTERNAL_TABNAME =
changing
ct_fieldcat = gt_fieldcat
exceptions
inconsistent_interface = 1
program_error = 2
others = 3 .
data: lt_drop type lvc_t_drop,
ls_drop type lvc_s_drop.
ls_drop-handle = '1'. " 功能F: 设定某列为下拉列表
ls_drop-value = 'A'. " 功能F: 设定某列为下拉列表
append ls_drop to lt_drop. " 功能F: 设定某列为下拉列表
ls_drop-handle = '1'. " 功能F: 设定某列为下拉列表
ls_drop-value = 'B'. " 功能F: 设定某列为下拉列表
append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
ls_drop-handle = '2'." 功能F: 设定某列为下拉列表
ls_drop-value = 'ZZZ'." 功能F: 设定某列为下拉列表
append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
ls_drop-handle = '2'." 功能F: 设定某列为下拉列表
ls_drop-value = 'YYY'." 功能F: 设定某列为下拉列表
append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
ls_drop-handle = '2'." 功能F: 设定某列为下拉列表
ls_drop-value = 'XXX'." 功能F: 设定某列为下拉列表
append ls_drop to lt_drop." 功能F: 设定某列为下拉列表
loop at gt_fieldcat into gs_fieldcat.
if gs_fieldcat-fieldname = 'ANAME'.
gs_fieldcat-emphasize = 'C501'.
gs_fieldcat-drdn_hndl = '1'. " 功能F: 设定某列为下拉列表
gs_fieldcat-edit = 'X'. " 功能E: 设定某列可输入
modify gt_fieldcat from gs_fieldcat.
elseif gs_fieldcat-fieldname = 'BNAME'.
gs_fieldcat-emphasize = 'C501'.
gs_fieldcat-drdn_hndl = '2'. " 功能F: 设定某列为下拉列表
gs_fieldcat-edit = 'X'. " 功能E: 设定某列可输入
modify gt_fieldcat from gs_fieldcat.
endif.
endloop.
call method g_grid->set_drop_down_table " 功能F: 设定某列为下拉列表
exporting
it_drop_down = lt_drop.
* it_drop_down_alias =
.
create object gr_event.
set handler gr_event->handle_double for g_grid. " 功能I: 设定响应双击
set handler gr_event->handle_toolbar for g_grid. " 功能H: 设定增加自定义ALV工具栏的按钮
set handler gr_event->handle_user_command for g_grid. " 功能J: 设定响应user command
set handler gr_event->handle_top for g_grid. " 功能K: 设定TOP_OF_PAGE
ls_layout-zebra = 'X'. " 功能B: 设定layout
ls_layout-grid_title = 'USER'. " 功能B: 设定layout
ls_layout-smalltitle = 'X'. " 功能B: 设定layout
ls_layout-box_fname = 'CHECKBOX'." 功能B: 设定layout
ls_layout-sel_mode = ''. " 功能B: 设定layout
* ls_layout-no_TOOLBAR = 'X'.
data gs_exclude type ui_func. " 功能G: 设定屏蔽标准ALV 工具栏的按钮
gs_exclude = cl_gui_alv_grid=>mc_mb_sum. " 功能G: 设定屏蔽标准ALV 工具栏的按钮
append gs_exclude to gt_exclude. " 功能G: 设定屏蔽标准ALV 工具栏的按钮
call method g_grid->set_table_for_first_display " 功能A: 基本显示
exporting
* i_buffer_active =
* i_bypassing_buffer =
* i_consistency_check =
* i_structure_name =
* is_variant =
* i_save = 'X'
* i_default = 'X'
is_layout = ls_layout " 功能B: 设定layout
* is_print =
* it_special_groups =
it_toolbar_excluding = gt_exclude " 功能G: 设定屏蔽标准ALV 工具栏的按钮
* it_hyperlink =
* it_alv_graphics =
* it_except_qinfo =
* ir_salv_adapter =
changing
it_outtab = gt_user[]
it_fieldcatalog = gt_fieldcat[] " 功能C: 设定fieldcat
* it_sort =
* it_filter =
exceptions
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
others = 4
.
else.
call method g_grid->refresh_table_display " 功能A: 基本显示
* EXPORTING
* is_stable =
* i_soft_refresh =
exceptions
finished = 1
others = 2
.
endif.
endmodule. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form frm_get_data .
select * from usr02 into table gt_user.
endform. " FRM_GET_DATA
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0100 input.
save_ok = ok_code.
clear ok_code.
case save_ok.
when 'BACK'.
leave to screen 0.
when 'SO_UP'.
perform frm_hide_toolbar.
when 'CH_CAT'.
perform frm_change_caption.
when others.
endcase.
endmodule. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form FRM_SORT_UP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form frm_hide_toolbar .
data: ls_layout type lvc_s_layo.
call method g_grid->get_frontend_layout
importing
es_layout = ls_layout.
if ls_layout-no_toolbar = 'X'.
ls_layout-no_toolbar = ''.
else.
ls_layout-no_toolbar = 'X'.
endif.
call method g_grid->set_frontend_layout
exporting
is_layout = ls_layout.
endform. " FRM_SORT_UP
*&---------------------------------------------------------------------*
*& Form FRM_CHANGE_CAPTION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form frm_change_caption .
data: lt_fieldcat type lvc_t_fcat,
ls_fieldcat like line of lt_fieldcat.
call method g_grid->get_frontend_fieldcatalog
importing
et_fieldcatalog = lt_fieldcat.
loop at lt_fieldcat into ls_fieldcat.
if ls_fieldcat-fieldname = 'BNAME'.
ls_fieldcat-coltext = 'X'.
ls_fieldcat-seltext = 'SELECT'.
modify lt_fieldcat from ls_fieldcat.
endif.
endloop.
call method g_grid->set_frontend_fieldcatalog
exporting
it_fieldcatalog = lt_fieldcat.
endform. " FRM_CHANGE_CAPTION