OO ALV 详解
创建用户对话屏幕,在屏幕上绘制一个用户自定义控件区域,然后以此基础来创建 cl_gui_custom_container容器实例,最后以此容器实例来创建cl_gui_alv_grid实例,调用其实例方法set_table_for_first_dispaly。
这就是用户自定义控件区域、容器、ALV控件三者之间的关系。
1.在屏幕上绘制一个用户自定义控件区域(属性)
2. (元素清单)
3.(逻辑流)
4.完整代码
*&---------------------------------------------------------------------* *& Report ZTEST_OO_ALV *& *&---------------------------------------------------------------------* *&OO ALV 最详实例 *& *&---------------------------------------------------------------------* report ztest_oo_alv. data:begin of gs_scarr. include structure scarr. data:checkbox type c. data end of gs_scarr. data gt_scarr like table of gs_scarr. field-symbols <scarr> like gs_scarr. data: gt_exclude type ui_functions. "隐藏标准按钮 data:gs_layout type lvc_s_layo, gt_fieldcat type lvc_t_fcat, "ALV输出 gs_fieldcat type lvc_s_fcat, "ALV 容器 gcl_alv type ref to cl_gui_alv_grid, gcl_container type ref to cl_gui_custom_container. data:gs_stable type lvc_s_stbl. data:ok_code like sy-ucomm. data:gs_toolbar type stb_button. "在ALV工具栏中新增自定义按钮 class cl_event_handle definition. "定义事件处理类 public section. "初始化ALV工具栏对象事件,如增加按钮并设定属性 methods handle_toolbar for event toolbar of cl_gui_alv_grid importing e_object e_interactive. "该事件用于在下ALV工具栏的下拉菜单按钮中增加选项 methods handle_menu_button for event menu_button of cl_gui_alv_grid importing e_object e_ucomm. "ALV工具栏按钮的点击事件 methods handle_user_command for event user_command of cl_gui_alv_grid importing e_ucomm. "ALV表格双击事件 methods handle_double_click for event double_click of cl_gui_alv_grid importing e_row e_column es_row_no. "ALV字段热点单击事件 methods handle_hotspot_click for event hotspot_click of cl_gui_alv_grid importing e_row_id e_column_id es_row_no. "数据改变触发 methods handle_data_changed for event data_changed of cl_gui_alv_grid importing er_data_changed. "覆盖预设功能FunCode methods handle_before_user_command for event before_user_command of cl_gui_alv_grid importing e_ucomm. endclass. class cl_event_handle implementation."事件处理类实现部分 method handle_toolbar. gs_toolbar-function = 'SET_ALL'."按钮的FunctionCode gs_toolbar-icon = icon_display."按钮图标 gs_toolbar-text = '全选'."按钮标签 gs_toolbar-butn_type = '0'."定义按钮类型,0为标准按钮,具体取值可参考这里 append gs_toolbar to e_object->mt_toolbar."添加按钮到工具栏中 gs_toolbar-function = 'CANCEL_ALL'."按钮的FunctionCode gs_toolbar-icon = icon_display."按钮图标 gs_toolbar-text = '取消全选'."按钮标签 gs_toolbar-butn_type = '0'."定义按钮类型,0为标准按钮,具体取值可参考这里 append gs_toolbar to e_object->mt_toolbar."添加按钮到工具栏中 gs_toolbar-function = 'B_LIST'."按钮的FunctionCode gs_toolbar-quickinfo = '数据处理'."按钮的冒泡提示 gs_toolbar-icon = icon_biw_report_view."按钮图标 gs_toolbar-text = '数据处理'."按钮标签 gs_toolbar-butn_type = '1'."定义按钮类型,1为下拉菜单按钮 append gs_toolbar to e_object->mt_toolbar."添加下拉菜单按钮到工具栏中 endmethod. method handle_menu_button. if e_ucomm = 'B_LIST'."给下拉菜单按钮增加选项,可以多次调用该方法以增加多行 call method e_object->add_function exporting icon = icon_display fcode = 'DELETE' "字菜单按钮的FunCode text = '删除所选数据1'. endif. endmethod. method handle_user_command. case e_ucomm. when 'SET_ALL'. loop at gt_scarr assigning <scarr>. <scarr>-checkbox = 'X'. endloop. when 'CANCEL_ALL'. loop at gt_scarr assigning <scarr>. <scarr>-checkbox = ''. endloop. when 'DELETE'. delete gt_scarr where checkbox = 'X'. when others. endcase. perform prm_refresh_table. endmethod. method handle_double_click. read table gt_scarr into gs_scarr index es_row_no-row_id. message i001(00) with '当前行:' es_row_no-row_id ',航线代码:' gs_scarr-carrid. endmethod. method handle_hotspot_click. read table gt_scarr into gs_scarr index es_row_no-row_id. message i001(00) with ',航线代码:' gs_scarr-carrid. endmethod. method handle_data_changed. "数据变化实现方法 data: ls_mod_cell type lvc_s_modi, lv_value type lvc_value. clear gs_scarr. loop at er_data_changed->mt_good_cells into ls_mod_cell. read table gt_scarr into gs_scarr index ls_mod_cell-row_id. "修改的行 endloop. perform prm_refresh_table. endmethod. method handle_before_user_command. case e_ucomm. when '&PRINT_BACK'. call method gcl_alv->set_user_command exporting i_ucomm = 'SET_ALL'. "指向自定义功能码 endcase. endmethod. endclass. data: event_handle type ref to cl_event_handle."定义类对象的引用 start-of-selection. perform pfm_get_data. end-of-selection. if gt_scarr[] is not initial. call screen 100. else. message '没查询到数据!' type 'E'. endif. form pfm_get_data . select * from scarr into table gt_scarr. endform. " GET_DATA module status_0100 output. *从tcode (SE41) 程序SAPLKKBL — 状态 STANDARD_FULLSCREEN 直接拷贝标准GUI set pf-status 'T001'. * SET TITLEBAR 'xxx'. perform exclude_status. endmodule. " STATUS_0100 OUTPUT module alv_init output. if gcl_alv is initial. perform prm_set_fieldcat. perform prm_alv_display. else. *刷新并重新显示ALV,当输出内表数据变更、或其他ALV设置变化时 perform prm_refresh_table. endif. endmodule. " ALV_INIT OUTPUT module user_command_0100 input. data(lv_code) = ok_code. clear ok_code. case lv_code. when '&F03' or '&F12' or '&F15'. leave to screen 0. when others. endcase. endmodule. " USER_COMMAND_0100 INPUT form prm_set_fieldcat . define fieldcat. Clear gs_fieldcat. gs_fieldcat-fieldname = &1. gs_fieldcat-SCRTEXT_M = &2. gs_fieldcat-EDIT = &3. "是否可编辑 gs_fieldcat-REF_FIELD = &4. gs_fieldcat-REF_TABLE = &5. "是否可编辑 gs_fieldcat-LOWERCASE = 'X'. IF &1 = 'CHECKBOX'. gs_fieldcat-CHECKBOX = 'X'. Elseif &1 = 'CARRID'. gs_fieldcat-HOTSPOT = 'X'. ENDIF. append gs_fieldcat to gt_fieldcat. end-of-definition. fieldcat: 'CHECKBOX' '选择' 'X' '' '', 'CARRID' '航线代码' '' '' '', 'CARRNAME' '航班名称' '' '' '', 'CURRCODE' '货币' '' 'CURRCODE' 'SCARR', 'URL' 'URL地址' 'X' 'URL' 'SCARR'. gs_layout-zebra = 'X'. gs_layout-cwidth_opt = 'X'. "优化列宽 gs_layout-no_rowmark = 'X'. "选择模式 删除GRID的行选择按钮 * gs_layout-box_fname = 'CHECKBOX'. "显示 * gs_layout-no_toolbar = 'X'. "隐藏工具条 gs_layout-sel_mode = 'A'. "选择模式 endform. " PRM_SET_FIELDCAT form prm_alv_display. *创建容器 create object gcl_container exporting container_name = 'CC'. create object gcl_alv exporting i_parent = gcl_container. "为ALV按钮注册监听事件 create object :event_handle. set handler :event_handle->handle_toolbar for gcl_alv, event_handle->handle_menu_button for gcl_alv, event_handle->handle_user_command for gcl_alv, event_handle->handle_data_changed for gcl_alv, event_handle->handle_hotspot_click for gcl_alv, event_handle->handle_double_click for gcl_alv, event_handle->handle_before_user_command for gcl_alv. *handle_data_changed call method gcl_alv->register_edit_event exporting * i_event_id = cl_gui_alv_grid=>mc_evt_enter. "按回车触发 i_event_id = cl_gui_alv_grid=>mc_evt_modified. "单元格失去焦点触发 *显示ALV call method gcl_alv->set_table_for_first_display exporting * i_buffer_active = * i_bypassing_buffer = * i_consistency_check = * i_structure_name = * is_variant = * i_save = * i_default = 'X' is_layout = gs_layout * is_print = * it_special_groups = it_toolbar_excluding = gt_exclude * it_hyperlink = * it_alv_graphics = * it_except_qinfo = * ir_salv_adapter = changing it_outtab = gt_scarr[] it_fieldcatalog = gt_fieldcat * it_sort = * it_filter = * exceptions * invalid_parameter_combination = 1 * program_error = 2 * too_many_lines = 3 * others = 4 . *调用此方法才能激活工具栏上增加的自定义按钮 call method gcl_alv->set_toolbar_interactive. endform. form prm_refresh_table . *IS_STABLE 刷新的稳定性,有2 个参数,一个是行,`一个是列。如果设置了相应的值,那么对应的行或者列,在刷新的时候将会保持稳定(就是滚动条保持不动) *I_SOFT_REFRESH软刷新,这个参数只是在异常情况下被使用,如果设置了这个参数,任何创建的合计,任何排序次序,任何为了显示数据而设置的过滤都将保持不变。 gs_stable-row = 'X'. gs_stable-col = 'X'. call method gcl_alv->refresh_table_display exporting is_stable = gs_stable * i_soft_refresh = * exceptions * finished = 1 * others = 2 . if sy-subrc <> 0. * Implement suitable error handling here endif. endform. " PRM_REFRESH_TABLE *&---------------------------------------------------------------------* *& Form EXCLUDE_STATUS *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* form exclude_status . data: ls_exclude type ui_func. ls_exclude = cl_gui_alv_grid=>mc_fc_check . append ls_exclude to gt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_refresh . 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_append_row . 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_delete_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_move_row . append ls_exclude to gt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste . 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_undo . 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_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. endform. " exclude_tb_functions
abap菜鸟,记录学习笔记。才疏学浅,如有错误,请直接在评论区留言。