DATA: gr_alvgrid TYPE REF TO cl_gui_alv_grid ,"ALV对象 gt_fieldcat TYPE lvc_t_fcat , "ALV字段控制 gs_layout TYPE lvc_s_layo , "ALV布局控制 gs_variant TYPE disvariant, "当前程序 alv_container TYPE scrfname VALUE 'T_COL', "屏幕元素名称,ALV展示数据存放在此容器中 alv_custom_container TYPE REF TO cl_gui_custom_container,ALV容器对象 *定义一个类 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_double_click "方法handle_double_click用于处理cl_gui_alv_grid类中的双击事件double_click FOR EVENT double_click of cl_gui_alv_grid IMPORTING e_row e_column. PRIVATE SECTION. ENDCLASS *方法的实现 CLASS lcl_event_handler IMPLEMENTATION. METHOD handle_double_click. PERFORM handle_double_click USING e_row e_column es_row_no. "定义处理双击事件方法, 如果双机alv某一行时,需要弹出子窗口中的alv的话这边,可以在此方法中获取需要显示的数据,还要类似于CALL SCREEN 0200 STARTING AT 10 5 ENDING AT 100 25.call出子屏幕 ENDMETHOD. "handle_double_click ENDCLASS. DATA: gr_event_handler TYPE REF TO lcl_event_handler. "定义对象引用,用于双击事件 *alv实现 gs_variant-report = sy-repid. IF gr_alvgrid IS INITIAL. CREATE OBJECT gr_alvgrid "创建gr_alvgrid对象 EXPORTING i_parent = cl_gui_container=>screen0. CREATE OBJECT gr_event_handler. 创建gr_event_handler对象 SET HANDLER gr_event_handler->handle_double_click FOR gr_alvgrid. "注册处理双机事件的方法 PERFORM display_alv. "ALV实现 ELSE. call METHOD gr_alvgrid->refresh_table_display. 刷新alv报表 ENDIF. FORM DISPLAY_ALV . "调用ALV的实现方法 CALL METHOD gr_alvgrid->set_table_for_first_display EXPORTING is_variant = gs_variant "当前程序 i_save = 'A' is_layout = gs_layout "ALV布局 CHANGING it_outtab = gt_zc_main[] "内表 it_fieldcatalog = gt_fieldcat "需要输出的字段 EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 3 OTHERS = 4. IF sy-subrc <> 0. ENDIF. ENDFORM
这边还要补充一些:
e_row TYPE lvc_s_row, "alv的行,如果要读取alv内表指定行时,可以read table 内表 index e_row-index.这样写 e_column TYPE lvc_s_col, "alv的列 es_row_no TYPE lvc_s_roid.
alv对象的析构函数free,也是类cl_gui_alv_grid中的,释放各实例化对象的内存,一般用在退出程序时,比如GUI界面点击 退出按钮时候
CALL METHOD SUB_GR_ALVGRID->FREE EXCEPTIONS CNTL_ERROR = 1 CNTL_SYSTEM_ERROR = 2 others = 3 . IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. CLEAR: sub_gr_alvgrid, sub_gs_layout,sub_gs_variant,sub_gt_fieldcat. LEAVE TO SCREEN 0.
还有排除不必要的按钮操作:下面有个维护表排除不必要按钮部分的 实例
gt_exclude TYPE ui_functions,"存放排除按钮的内表 ***排除不要的按钮 PERFORM exclude_tb_functions USING gt_exclude. FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions. DATA ls_exclude TYPE ui_func. ls_exclude = cl_gui_alv_grid=>mc_fc_current_variant. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_save_variant. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_average. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_load_variant. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_minimum. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_maximum. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_maintain_variant. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_views. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_graph. APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_sum. * APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_subtot. APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_filter. * APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_sort_dsc. * APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_sort_asc. * APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_check. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_detail. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_refresh. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo. APPEND ls_exclude TO pt_exclude. IF p_insert <> 'X' . ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row. APPEND ls_exclude TO pt_exclude. ELSE. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row. APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row. APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy. * APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row. * APPEND ls_exclude TO pt_exclude. * ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row. * APPEND ls_exclude TO pt_exclude. ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row. APPEND ls_exclude TO pt_exclude. ENDIF. ENDFORM. ALV 展示的时候传入排除不必要按钮的参数 ***展示数据 CALL METHOD gr_alvgrid->set_table_for_first_display EXPORTING is_variant = gs_variant i_save = 'A' is_layout = gs_layout it_toolbar_excluding = gt_exclude "排除不必的按钮 CHANGING it_outtab = gt_data it_fieldcatalog = gt_fieldcat EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 3 OTHERS = 4.
对于alv中的布局定义:
示例1:维护表中的示例
PERFORM prepare_layout CHANGING gs_layout ."设置layout FORM prepare_layout CHANGING ps_layout TYPE lvc_s_layo. ps_layout-zebra = 'X' . "可选颜色 ps_layout-smalltitle = 'X' . "标题大小 ps_layout-no_toolbar = ' '. "显示工具栏 IF p_disp = 'X' OR p_modify = 'X'. ps_layout-no_rowmark = 'X'. "禁用行选择 ENDIF. ENDFORM.
示例2:
PERFORM prepare_layout CHANGING gs_layout. FORM PREPARE_LAYOUT CHANGING P_GS_LAYOUT TYPE lvc_s_layo. p_gs_layout-zebra = ' ' . p_gs_layout-grid_title = text-001 . "ALV 控制: 标题栏文本 p_gs_layout-smalltitle = 'X' . "可选颜色 p_gs_layout-no_toolbar = ' '. "显示工具栏 p_gs_layout-no_rowmark = 'X'. "禁用行选择 p_gs_layout-cwidth_opt = 'X'. "优化列宽 ENDFORM.
字段控制: 示例1:
gt_fieldcat TYPE lvc_t_fcat,"需要显示的字段 PERFORM prepare_field_catalog CHANGING gt_fieldcat. "gt_fieldcat FORM PREPARE_FIELD_CATALOG CHANGING PT_FIELDCAT TYPE lvc_t_fcat. CLEAR: PT_FIELDCAT. PERFORM fill_field_catalog USING pt_fieldcat 1 'VKORG' '' '销售组织'. "销售组织 ENDFORM. FORM fill_field_catalog USING pt_fieldcat TYPE lvc_t_fcat col_pos fieldname datatype coltext . DATA ls_fieldcat TYPE lvc_s_fcat. CLEAR ls_fieldcat . ls_fieldcat-fieldname = fieldname . "字段名称 ls_fieldcat-coltext = coltext. "ALV 控制: 列标 ls_fieldcat-datatype = datatype. "ABAP 字典中的数据类型 ls_fieldcat-col_pos = col_pos. "ALV 控制: 输出列 IF ls_fieldcat-fieldname = 'KUNNR'. ls_fieldcat-REF_FIELD = 'KUNNR'. "ALV 控制: 内部表字段的参考字段名称 ls_fieldcat-REF_TABLE = 'KNA1'. "ALV 控制: 内部表字段的参考表名称 ENDIF. APPEND ls_fieldcat TO pt_fieldcat . ENDFORM.
示例2:可以通过函数实现:
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' EXPORTING i_structure_name = g_tabname "表结构 i_client_never_display = 'X' CHANGING ct_fieldcat = pt_fieldcat "字段控制表 EXCEPTIONS inconsistent_interface = 1 program_error = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.
可以循环pt_fieldcat,在赋值,
*UserExit,用于自定义Field Catalog PERFORM userexit_fieldcat CHANGING pt_fieldcat. FORM userexit_fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat. DATA: ls_fieldcat TYPE lvc_s_fcat. LOOP AT pt_fieldcat INTO ls_fieldcat. CASE ls_fieldcat-fieldname. WHEN 'BUKRS'. ls_fieldcat-coltext = '分公司' . ls_fieldcat-col_pos = 1. ls_fieldcat-outputlen = 8. WHEN 'ORGTYPE'. ls_fieldcat-coltext = '部门类型' . ls_fieldcat-col_pos = 3. ls_fieldcat-outputlen = 8. WHEN 'ORGID'. ls_fieldcat-coltext = '部门编码' . ls_fieldcat-col_pos = 4. ls_fieldcat-outputlen = 10. WHEN 'LVORM'. ls_fieldcat-coltext = '删除标记' . ls_fieldcat-col_pos = 6. ls_fieldcat-outputlen = 8. WHEN 'BUNO'. ls_fieldcat-coltext = '创建人员' . ls_fieldcat-col_pos = 7. ls_fieldcat-outputlen = 12. WHEN 'BUDAT'. ls_fieldcat-coltext = '创建日期' . ls_fieldcat-col_pos = 8. ls_fieldcat-outputlen = 8. WHEN 'BUTIME'. ls_fieldcat-coltext = '创建时间' . ls_fieldcat-col_pos = 9. ls_fieldcat-outputlen = 8. WHEN 'UPNO'. ls_fieldcat-coltext = '修改人员' . ls_fieldcat-col_pos = 10. ls_fieldcat-outputlen = 12. WHEN 'UPDAT'. ls_fieldcat-coltext = '修改日期' . ls_fieldcat-col_pos = 11. ls_fieldcat-outputlen = 8. WHEN 'UPTIME'. ls_fieldcat-coltext = '修改时间' . ls_fieldcat-col_pos = 12. ls_fieldcat-outputlen = 8. ENDCASE. IF p_insert = 'X'. IF ls_fieldcat-fieldname = 'BUKRS' OR ls_fieldcat-fieldname = 'ORGTYPE' OR ls_fieldcat-fieldname = 'ORGID'. ls_fieldcat-edit = 'X'. ENDIF. " IF ls_fieldcat-fieldname = 'LVORM'. " ls_fieldcat-no_out = c_flg_on. " ENDIF. ELSEIF p_modify = 'X'. IF ls_fieldcat-fieldname = 'ORGID' OR ls_fieldcat-fieldname = 'LVORM'. ls_fieldcat-edit = 'X'. ENDIF. ENDIF. MODIFY pt_fieldcat FROM ls_fieldcat. ENDLOOP. ENDFORM. "特殊的要显示的可以额外添加