REPORT YTEST20160620002.
INCLUDE .
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
DATA: OK_CODE TYPE SY-UCOMM,
SAVE_OK TYPE SY-UCOMM.
DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.
DATA: WA_SCHOOL TYPE TABLE OF YTJAYSCHOOL,
A_SCHOOL LIKE YTJAYSCHOOL.
SELECT * INTO TABLE WA_SCHOOL FROM YTJAYSCHOOL.
DATA:WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
CALL SCREEN 100.
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_TOOLBAR
FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT E_INTERACTIVE,
HANDLE_USER_COMMAND
FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
ENDCLASS.
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_TOOLBAR.
DATA:LS_TOOLBAR TYPE STB_BUTTON.
CLEAR LS_TOOLBAR-BUTN_TYPE.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
CLEAR LS_TOOLBAR.
MOVE 'SHOW_DETA' TO LS_TOOLBAR-FUNCTION.
MOVE ICON_PPE_VNODE TO LS_TOOLBAR-ICON.
MOVE '学校明细显示' TO LS_TOOLBAR-QUICKINFO.
MOVE '学校明细显示' TO LS_TOOLBAR-TEXT.
MOVE '' TO LS_TOOLBAR-DISABLED.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD.
METHOD HANDLE_USER_COMMAND.
DATA:LT_ROWS TYPE LVC_T_ROW.
CASE E_UCOMM.
WHEN 'SHOW_DETA'.
CALL METHOD ALV_GRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = LT_ROWS.
CALL METHOD CL_GUI_CFW=>FLUSH.
IF SY-SUBRC = 0.
MESSAGE S005(YMESS) WITH '已选择行'.
PERFORM MESSDETA TABLES LT_ROWS.
ENDIF.
ENDCASE.
ENDMETHOD.
ENDCLASS.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'STATUS1'.
IF WA_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT WA_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = WA_CONTAINER.
CREATE OBJECT ALV_GRID
EXPORTING I_PARENT = WA_CUSTOM_CONTAINER.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'YTJAYSCHOOL'
CHANGING
IT_OUTTAB = WA_SCHOOL.
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_USER_COMMAND FOR ALV_GRID.
SET HANDLER EVENT_RECEIVER->HANDLE_TOOLBAR FOR ALV_GRID.
CALL METHOD ALV_GRID->SET_TOOLBAR_INTERACTIVE.
ENDIF.
ENDMODULE.
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'CANCEL'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE.
FORM MESSDETA TABLES P_ET_INDEX_ROWS STRUCTURE LVC_S_ROW.
DATA:LS_SELECTED_LINE LIKE LVC_S_ROW,
LF_ROW_INDEX TYPE LVC_INDEX.
DATA:S1(200) TYPE C,
S2(3) TYPE C.
S2 = '-'.
LOOP AT P_ET_INDEX_ROWS INTO LS_SELECTED_LINE.
LF_ROW_INDEX = LS_SELECTED_LINE-INDEX.
READ TABLE WA_SCHOOL INDEX LF_ROW_INDEX INTO A_SCHOOL.
S1 = '选择行内容'.
CONCATENATE S1 A_SCHOOL-YSH_NAME A_SCHOOL-YSH_ADDR
INTO S1 SEPARATED BY S2.
MESSAGE I005(YMESS) WITH S1.
ENDLOOP.
ENDFORM.
5、处理ALV双击事件
主程序代码以下
REPORT YTEST20160620003.
*功能码返回值
DATA:OK_CODE TYPE SY-UCOMM,
SAVE_OK TYPE SY-UCOMM.
*定义内表,变量需要传递,不加HEADER LINE
DATA WA_SCHOOL TYPE TABLE OF YTJAYSCHOOL.
*内表赋值
SELECT * INTO TABLE WA_SCHOOL FROM YTJAYSCHOOL.
*定义窗口定制控件,定义ALV对象
DATA:WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
*定义事件类型
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
*定义事件
DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.
*直接调用窗口
CALL SCREEN 100.
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN.
ENDCLASS.
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_DOUBLE_CLICK.
DATA:LI_SCHOOL LIKE LINE OF WA_SCHOOL.
READ TABLE WA_SCHOOL INDEX E_ROW-INDEX INTO LI_SCHOOL.
*将行列等信息合并到字符串
DATA:S1(100) TYPE C.
* CONCATENATE '行:' E_ROW-INDEX '列名:' E_COLUMN-FIELDNAME INTO S1.
CONCATENATE S1 'SCHOOL NAME:' LI_SCHOOL-YSH_NAME INTO S1.
CONCATENATE S1 'SCHOOL ADDR:' LI_SCHOOL-YSH_ADDR INTO S1.
*在状态条显示单击的行与列信息
MESSAGE S208(00) WITH S1.
ENDMETHOD.
ENDCLASS.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'STATUS1'.
*如果窗口还没有创建ALV对象则创建它
IF WA_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT WA_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = WA_CONTAINER.
CREATE OBJECT ALV_GRID
EXPORTING I_PARENT = WA_CUSTOM_CONTAINER.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'YTJAYSCHOOL'
CHANGING
IT_OUTTAB = WA_SCHOOL.
*ALV对象分配双击事件
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_DOUBLE_CLICK
FOR ALV_GRID.
ENDIF.
ENDMODULE.
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'CANCEL'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE.输出界面以下图,双击时,注意状态条显示的信息:学校名称、学校地址的值。
6、通过ALV控件编辑内表和数据库更新
处理进程以下:
(1)设定ALV控件可以编辑;
(2)退出屏幕时将数据更新到内表;
(3)捕捉ALV控件的数据更改信息,将ALV控件的删除行信息保存到内表中;
(4)在输出时,比较删除行和最后的内表,删除重复的行;
(5)将数据更新到数据表。
主程序代码以下:
REPORT YTEST20160620004.
DATA:OK_CODE TYPE SY-UCOMM,
SAVE_OK TYPE SY-UCOMM.
TABLES YTJAYSCHOOL.
DATA LS_SCHOOL TYPE YTJAYSCHOOL.
DATA WA_SCHOOL TYPE TABLE OF YTJAYSCHOOL.
DATA WADEL_SCHOOL TYPE TABLE OF YTJAYSCHOOL.
SELECT * INTO TABLE WA_SCHOOL FROM YTJAYSCHOOL.
DATA:WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA WA_LAYOUT TYPE LVC_S_LAYO.
WA_LAYOUT-EDIT = 'X'.
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
TYPES: DEL_ROWS TYPE STANDARD TABLE OF YTJAYSCHOOL.
DATA: DDEL_ROWS TYPE STANDARD TABLE OF YTJAYSCHOOL.
METHODS:
HANDLE_DATA_CHANGED
FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
METHODS:
UPDATE_DELTA_TABLES
IMPORTING
PR_DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
METHODS:
GET_DELETED_ROWS
EXPORTING
DELETED_ROWS TYPE DEL_ROWS.
ENDCLASS.
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_DATA_CHANGED.
CALL METHOD UPDATE_DELTA_TABLES( ER_DATA_CHANGED ).
ENDMETHOD.
METHOD UPDATE_DELTA_TABLES.
DATA: L_DEL_ROW TYPE LVC_S_MOCE.
LOOP AT PR_DATA_CHANGED->MT_DELETED_ROWS INTO L_DEL_ROW.
READ TABLE WA_SCHOOL INTO LS_SCHOOL INDEX L_DEL_ROW-ROW_ID.
IF SY-SUBRC NE 0.
MESSAGE E208(00) WITH '处理毛病'.
ELSE.
APPEND LS_SCHOOL TO DDEL_ROWS.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD GET_DELETED_ROWS.
DELETED_ROWS = ME->DDEL_ROWS.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
CALL SCREEN 100.
WRITE / '---------------删除的内表记录---------------'.
WRITE / '____________________________________________'.
CALL METHOD EVENT_RECEIVER->GET_DELETED_ROWS
IMPORTING
DELETED_ROWS = WADEL_SCHOOL.
LOOP AT WADEL_SCHOOL INTO YTJAYSCHOOL.
WRITE:/ YTJAYSCHOOL-YSH_ID,YTJAYSCHOOL-YSH_NAME,YTJAYSCHOOL-YSH_ADDR.
ENDLOOP.
WRITE:/ '---------------更新后的内表记录---------------:'.
WRITE:/ '____________________________________________'.
LOOP AT WA_SCHOOL INTO YTJAYSCHOOL.
WRITE:/ YTJAYSCHOOL-YSH_ID,YTJAYSCHOOL-YSH_NAME,YTJAYSCHOOL-YSH_ADDR.
ENDLOOP.
* WHEN 'SAVE'.
* MODIFY YTJAYSCHOOL FROM TABLE WA_SCHOOL.
* IF SY-SUBRC NE 0.
* MESSAGE I005(YMESS) WITH '更新数据毛病'.
* EXIT.
* ELSE.
* MESSAGE I005(YMESS) WITH '更新数据OK'.
* ENDIF.
*
* DELETE YTJAYSCHOOL FROM TABLE DELA_SCHOOL.
* IF SY_SUBRC NE 0.
* MESSAGE I005(YMESS) WITH '更新数据毛病'.
* ELSE.
* MESSAGE I005(YMESS) WITH '更新数据OK'.
* ENDIF.
END-OF-SELECTION.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'STATUS1'.
IF WA_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT WA_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = WA_CONTAINER.
CREATE OBJECT ALV_GRID
EXPORTING I_PARENT = WA_CUSTOM_CONTAINER.
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR ALV_GRID.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'YTJAYSCHOOL'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = WA_SCHOOL.
ENDIF.
ENDMODULE.
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'CANCEL'.
DATA L_RET VALUE 'X'.
CALL METHOD ALV_GRID->CHECK_CHANGED_DATA
IMPORTING
E_VALID = L_RET.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.输出结果:
删除最后两行数据,点击“CANCEL"按钮,结果以下:
7、ALV Tree的使用
在SAP业务系统中,大量地使用了ALV Tree对象,该对象在表格基础上对同类数据进行归类,并对各分类能进行数据汇总。如图
以学校表(YTJAYSCHOOL)为例:
(1)以城市、学校分类;
(2)在右屏输出学校名称、学校地址,并控制其输出长度。
处理进程:
(1)建立程序和屏幕,在屏幕上建立定制控制对象,定义好逻辑流;
(2)建立好PAI、PBO事件;
(3)在PAI中定义建立定制控制对象,并建立ALV Tree对象;
(4)建立ALV Tree对象的标题;
(5)建立右屏输出字段清单、字段长度等内容;
(6)建立Tree分类字段清单及输出前后顺序;
(7)显示ALV Tree对象。
主程序代码:
REPORT YTEST20160621001.
DATA: OK_CODE TYPE SY-UCOMM,
SAVE_OK TYPE SY-UCOMM.
DATA: GB_FIELDCAT TYPE LVC_T_FCAT.
DATA: GB_SORTFLD TYPE LVC_T_SORT.
DATA WA_SCHOOL TYPE TABLE OF YTJAYSCHOOL.
SELECT * INTO TABLE WA_SCHOOL FROM YTJAYSCHOOL.
DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
ALV_GRID TYPE REF TO CL_GUI_ALV_TREE_SIMPLE,
WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
CALL SCREEN 100.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'STATUS1'.
IF WA_CUSTOM_CONTAINER IS INITIAL.
DATA LS_LIST_COMM TYPE SLIS_T_LISTHEADER.
DATA LS_ALIST_COMM TYPE SLIS_LISTHEADER.
LS_ALIST_COMM-TYP = 'H'.
LS_ALIST_COMM-INFO = 'MY ALV TREE Testing'.
APPEND LS_ALIST_COMM TO LS_LIST_COMM.
PERFORM BLDCAT.
PERFORM BLDSORTFLD.
CREATE OBJECT WA_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = WA_CONTAINER.
CREATE OBJECT ALV_GRID
EXPORTING I_PARENT = WA_CUSTOM_CONTAINER.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IT_LIST_COMMENTARY = LS_LIST_COMM
I_STRUCTURE_NAME = 'YTJAYSCHOOL'
CHANGING
IT_SORT = GB_SORTFLD
IT_FIELDCATALOG = GB_FIELDCAT
IT_OUTTAB = WA_SCHOOL.
CALL METHOD ALV_GRID->EXPAND_TREE
EXPORTING
I_LEVEL = 1.
ENDIF.
ENDMODULE.
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'CANCEL'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE.
FORM BLDCAT.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = 'YTJAYSCHOOL'
CHANGING
CT_FIELDCAT = GB_FIELDCAT.
DATA LS_FLDCAT TYPE LVC_S_FCAT.
LOOP AT GB_FIELDCAT INTO LS_FLDCAT.
CASE LS_FLDCAT-FIELDNAME.
WHEN 'YSH_NAME' OR 'YSH_ADDR'.
LS_FLDCAT-OUTPUTLEN = 15.
WHEN OTHERS.
LS_FLDCAT-NO_OUT = 'X'.
ENDCASE.
MODIFY GB_FIELDCAT FROM LS_FLDCAT.
ENDLOOP.
ENDFORM.
FORM BLDSORTFLD.
DATA LS_SORTFLD TYPE LVC_S_SORT.
LS_SORTFLD-SPOS = 1.
LS_SORTFLD-FIELDNAME = 'YCT_ID'.
LS_SORTFLD-UP = 'X'.
LS_SORTFLD-SUBTOT = 'X'.
APPEND LS_SORTFLD TO GB_SORTFLD.
LS_SORTFLD-SPOS = 2.
LS_SORTFLD-FIELDNAME = 'YSH_ID'.
LS_SORTFLD-UP = 'X'.
LS_SORTFLD-SUBTOT = 'X'.
APPEND LS_SORTFLD TO GB_SORTFLD.
* LS_SORTFLD-SPOS = 3.
* LS_SORTFLD-FIELDNAME = 'YSH_NAME'.
* LS_SORTFLD-UP = 'X'.
* LS_SORTFLD-SUBTOT = 'X'.
* APPEND LS_SORTFLD TO GB_SORTFLD.
*
* LS_SORTFLD-SPOS = 4.
* LS_SORTFLD-FIELDNAME = 'YSH_ADDR'.
* LS_SORTFLD-UP = 'X'.
* LS_SORTFLD-SUBTOT = 'X'.
* APPEND LS_SORTFLD TO GB_SORTFLD.
ENDFORM.