ABAP OOALV
一、搭建基本框架-界面
创建一个REPORT程序,参照CL_GUI_ALV_GRID创建对象,参照CL_GUI_CUSTOM_CONTAINER创建对象
取数
输出:创建FIELDCAT,把学生表中的所有字段,创建FIELDCAT,实例化容器、实例化ALV、调用方法SET_TABLE_FOR_FIRST_DISPALY
SE24类:CL_GUI_ALV_GRID
REPORT Z_G_08 MESSAGE-ID ZG_01.INCLUDE Z_G_08TOP.
INCLUDE Z_G_08F01.START-OF-SELECTION.
PERFORM GET_DATA.END-OF-SELECTION.
IF GT_STUDENT IS NOT INITIAL.PERFORM DISPLAY_DATA.ELSE.MESSAGE S001 DISPLAY LIKE 'E'.
ENDIF.
创建TOP和F01,并在F01中分别创建GET_DATA和DISPLAYDATA.
定义并创建选择界面
DATA GT_STUDENT TYPE TABLE OF ZSTUDENT_G_01.
DATA GS_STUDENT TYPE ZSTUDENT_G_01.SELECT-OPTIONS S_ZCODE FOR GS_STUDENT-ZCODE.
取数
FORM GET_DATA .SELECT *FROM ZTUDENT_G_01INTO CORRESPONDING FIELDS OF TABLE GT_STUDENTWHERE ZCODE IN S_ZCODE.
ENDFORM.
展示数据
FORM DISPLAY_DATA .PERFORM BUILD_FIELDCAT.PERFORM BUILD_ALV.
ENDFORM.
FORM BUILD_FIELDCAT .%%ADD_fieldcat:'ZCODE' '学号' '10','ZNAME' '姓名' '20','SEX' '性别' '5','ZSCHOOL' '学校' '10','WEIGHT' '体重' '15','ZUNIT' '单位' '5'.
ENDFORM.
面向对象的表单展示,参考类定义
基于实例的一个方法,所以需要给这个类创建一个对象
*OOALV DISPLAY
DATA GS_ALV TYPE REF TO CL_GUI_ALV_GRID.
调用方法
取消异常注释
搜索CONSTRUCTOR,查看参数,可以看到该值为必输项
DATA GS_PARENT TYPE REF TO CL_GUI_CONTAINER.
因为GS_PARENT的类型为类,所以也需要进行实例化
FORM BUILD_ALV .
*实例化CREATE OBJECT GS_PARENTEXPORTINGCLASSID = ''.CREATE OBJECT GS_ALVEXPORTINGI_PARENT = GS_PARENT.
ENDFORM.
但是一般不使用CL_GUI_CONTAINER,而使用CL_GUI_CUSTOM_CONTAINER
修改GS_PARENT的参照类型 ,可以查到CL_GUI_CUSTOM_CONTAINER这个类,也有一个必输项,修改如下:
FORM BUILD_ALV .
*实例化
*GS_PARENT的实例化--实例化容器 CREATE OBJECT GS_PARENTEXPORTINGCONTAINER_NAME = ''."界面中CUSTOMER CONTROL控件名称*将ALV植入到容器中 CREATE OBJECT GS_ALVEXPORTINGI_PARENT = GS_PARENT.
ENDFORM.
定义表单列格式
根据IT_FIELDCATLOG的类型,定义GT_FIELDCAT类型
DATA GT_FIELDCAT TYPE LVC_T_FCAT."列结构
DATA GS_FIELDCAT TYPE LVC_S_FCAT.
双击LVC_T_FCAT查看参数,通过宏完成内表的增加
*定义宏
DATA GV_POS TYPE I.
DEFINE %%ADD_FIELDCAT.CLEAR GS_FIELDCAT.GV_POS = GV_POS + 1.GS_FIELDCAT-COL_POS = GV_POS.GS_FIELDCAT-FIELDNAME = &1.GS_FIELDCAT-SCRTEXT_M = &2.GS_FIELDCAT-OUTPUTLEN = &3.CASE &2.WHEN 'WEIGH'.GS_FIELDCAT-QFIELDNAME = 'ZUNIT'.ENDCASE.APPEND GS_FIELDCAT TO GT_FIELDCAT.
END-OF-DEFINITION.
创建SCREEN,在布局中画一个定制控制,命名为GC_CON,决定了ALV输出的时候的位置
将定义的名字放在此处,同时调用屏幕
调用屏幕后不执行PERFORM,因此调用ALV要放在PBO中,将原先的PERFORM DISPLAY_DATA注释,并将其中的两个子程序放在PBO中
输出效果:
二、布局与按钮
1、单元格颜色设置
前提需要定义变量名
2、定义布局
DATA GS_LAYOUT TYPE LVC_S_LAYO.
TYPES:BEGIN OF GTY_STUDENT,BOX TYPE C LENGTH 1.INCLUDE STRUCTURE ZSTUDENT_G_01.
TYPES: FIELD_COLOR TYPE LVC_T_SCOL,FIELD_MODIFY TYPE LVC_T_STYL,END OF GTY_STUDENT.DATA GT_STUDENT TYPE TABLE OF GTY_STUDENT.
DATA GS_STUDENT TYPE GTY_STUDENT.
FORM build_layout .gs_layout-CWIDTH_opt = 'X'."优化列宽gs_layout-zebra = 'X'. "斑马纹gs_layout-keyhot = 'X'. "关键字的热点
* gs_layout-window_titlebar = '窗体抬头(layout)'.gs_layout-box_fname = 'BOX'."内表中字段名称 - 定义为当前行的选择按钮,选择时自动选x
* gs_layout-info_fieldname = 'LINE_COLOR'. "行颜色gs_layout-CTAB_fname = 'FIELD_COLOR'. "单元格颜色
* GS_LAYOUT-EDIT = 'X'."以表单为单位可修改"以列为单位可修改在FIELDCAT处添加gs_layout-stylefname = 'FIELD_MODIFY'."以单元格为单位可修改
ENDFORM.
如果修改列
以单元格进行设置
在内表中增加一个字段,类型为LVC_T_STYL,例如CELLSTL
然后进行赋值
进入BUILD_LAYOUT进行属性定义
gs_layout-stylefname = 'FIELD_MODIFY'."以单元格为单位可修改
因为优先级的问题可能不会执行成功,在FIELDCAT中增加,即可成功
2、增加按钮
定义一个0100的按钮组,并设置基本的按钮
创建一个抬头,双击填写抬头名
定义按钮功能,在EXIT中执行LEAVE PROGRAM
PROCESS BEFORE OUTPUT.
MODULE INIT_ALV.MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE EXIT AT EXIT-COMMAND.
MODULE USER_COMMAND_0100.
MODULE EXIT INPUT.
LEAVE PROGRAM.
ENDMODULE.MODULE USER_COMMAND_0100 INPUT.
CASE OK_CODE.WHEN 'BACK'.LEAVE TO SCREEN 0.WHEN 'EXIT'OR 'CANCEL'.LEAVE PROGRAM.
ENDCASE.
ENDMODULE.
声明OK_CODE,并在TOP创建一个变量进行传值
DATA OK_CODE TYPE SY-UCOMM.
三、表单的重新刷新
使用REFRESH_ALV
MODULE INIT_ALV OUTPUT.IF GS_ALV IS INITIAL.PERFORM CREATE_ALV.PERFORM BUILD_FIELDCAT.PERFORM BULID_LAYOUT.PERFORM BUILD_ALV.ELSE.PERFORM REFRESH_ALV.ENDIF.
ENDMODULE.
调用方法
在TOP中定义一下参数
DATA GS_STABLE TYPE LVC_S_STBL."固定
分别对STABLE中的参数定义,查看LVC_S_STBL的类型来定义参数
GS_STABLE-ROW = 'X'.GS_STABLE-COL = 'X'.
四、按钮隐藏
OOALV与FUNCTION ALV的按钮位置不一样,是两种按钮,与STATUS无关
思路:使用方法将按钮隐藏
FORM BUILD_exculde .CLEAR gt_exclude[].
*找到方法中对应的属性
* gs_exclude = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW."插入
* APPEND GS_EXCLUDE TO GT_EXCLUDE.
*
* gs_exclude = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW."删除
* APPEND GS_EXCLUDE TO GT_EXCLUDE.
*
* gs_exclude = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
* APPEND GS_EXCLUDE TO GT_EXCLUDE.
*
* gs_exclude = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.
* APPEND GS_EXCLUDE TO GT_EXCLUDE.*调用子程序
PERFORM append_alv_exclude_functions TABLES gt_exclude "隐藏按钮USING :cl_gui_alv_grid=>mc_fc_loc_insert_row,cl_gui_alv_grid=>MC_FC_LOC_delete_ROW,cl_gui_alv_grid=>MC_FC_LOC_append_ROW,cl_gui_alv_grid=>MC_FC_LOC_copy_ROW,cl_gui_alv_grid=>MC_FC_LOC_cut.
ENDFORM.
FORM append_alv_exclude_functions TABLES pt_exclude TYPE ui_functionsUSING p_exclude TYPE ui_func.APPEND p_exclude TO pt_exclude.
ENDFORM .
五、用户交互事件
CLASS类实现按钮的添加
先声明一个本地类
INCLUDE Z_G_08_CLS.
定义类,进行方法的声明
CLASS lcl_app_compt DEFINITION.PUBLIC SECTION.
*按钮方法METHODS handle_toolbar FOR EVENT toolbarOF cl_gui_alv_gridIMPORTING e_object.METHODS handle_user_command FOR EVENT user_commandOF cl_gui_alv_gridIMPORTING e_ucomm.METHODS handle_hotspot_click FOR EVENT hotspot_clickOF cl_gui_alv_gridIMPORTING e_row_ide_column_ides_row_no.METHODS handle_double_click FOR EVENT double_clickOF cl_gui_alv_gridIMPORTING e_rowe_column.METHODS HANDLE_DATA_CHANGED FOR EVENT DATA_CHANGEDOF cl_gui_alv_gridIMPORTING ER_DATA_CHANGED.ENDCLASS.
CLASS lcl_app_compt IMPLEMENTATION.
* HANDLE TOOLBARMETHOD handle_toolbar.PERFORM fm_handle_toolbar USING e_object.ENDMETHOD.* HANDLE user_commandMETHOD handle_user_command.PERFORM fm_user_command USING e_ucomm.ENDMETHOD.* HANDLE hotspot_clickMETHOD handle_hotspot_click.PERFORM fm_hotspot_click USING e_row_id e_column_id.ENDMETHOD.* HANDLE double_clickMETHOD handle_double_click.PERFORM fm_double_click USING e_row e_column.ENDMETHOD.* HANDLE_DATA_CHANGEDMETHOD HANDLE_DATA_CHANGED .PERFORM fm_DATA_CHANGED USING ER_DATA_CHANGED->mt_mod_cells.ENDMETHOD.
ENDCLASS.
FORM fm_handle_toolbar USING p_object TYPE REF TO cl_alv_event_toolbar_set."向mt_toolbar内表中增加行,增加按钮DATA ls_button TYPE stb_button.CLEAR: ls_button.ls_button-butn_type = '3'."插入分割线APPEND ls_button TO p_object->mt_toolbar."传入参数E_OBJECTCLEAR: ls_button.ls_button-function = 'CALLSF'.ls_button-text = '调用sf'.ls_button-icon = icon_failure.APPEND ls_button TO p_object->mt_toolbar.CLEAR: ls_button.ls_button-function = 'EDIT'.ls_button-text = '修改选中行'.ls_button-icon = icon_toggle_display_change.APPEND ls_button TO p_object->mt_toolbar.CLEAR: ls_button.ls_button-function = 'DOWNLOAD'.ls_button-text = '下载表单'.ls_button-icon = icon_layout_control.APPEND ls_button TO p_object->mt_toolbar.ENDFORM.