(7)ALV Tree的使用
REPORT YTEST20160617001.
*定义内表
DATA WA_SCHOOL LIKE TABLE OF YTJAYSCHOOL WITH HEADER LINE.
*内表赋值
SELECT * INTO TABLE WA_SCHOOL FROM YTJAYSCHOOL.
*通过数据字典结构显示ALV
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = 'YTJAYSCHOOL'
TABLES
T_OUTTAB = WA_SCHOOL
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
输出结果:
REPORT YTEST20160617002.
*ALV使用到的类库
TYPE-POOLS:SLIS.
*一列描述
DATA WA_ALV_FIELD TYPE SLIS_FIELDCAT_ALV.
*列描述内表,列清单
DATA WA_ALV_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
*定义内表
DATA WA_SCHOOL LIKE TABLE OF YTJAYSCHOOL WITH HEADER LINE.
*内表赋值
SELECT * INTO TABLE WA_SCHOOL FROM YTJAYSCHOOL.
*定义第一到第四个字段
WA_ALV_FIELD-COL_POS = 1.
WA_ALV_FIELD-FIELDNAME = 'YCT_ID'.
WA_ALV_FIELD-SELTEXT_M = '城市编号'.
APPEND WA_ALV_FIELD TO WA_ALV_FIELDCAT.
WA_ALV_FIELD-COL_POS = 2.
WA_ALV_FIELD-FIELDNAME = 'YSH_ID'.
WA_ALV_FIELD-SELTEXT_M = '学校编号'.
APPEND WA_ALV_FIELD TO WA_ALV_FIELDCAT.
WA_ALV_FIELD-COL_POS = 3.
WA_ALV_FIELD-FIELDNAME = 'YSH_NAME'.
WA_ALV_FIELD-SELTEXT_M = '学校名称'.
APPEND WA_ALV_FIELD TO WA_ALV_FIELDCAT.
WA_ALV_FIELD-COL_POS = 4.
WA_ALV_FIELD-FIELDNAME = 'YSH_ADDR'.
WA_ALV_FIELD-SELTEXT_M = '学校地址'.
APPEND WA_ALV_FIELD TO WA_ALV_FIELDCAT.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
IT_FIELDCAT = WA_ALV_FIELDCAT
TABLES
T_OUTTAB = WA_SCHOOL.
输出结果:
PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.
REPORT YTEST20160620001.
*功能码返回值
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.
*ALVDATA是屏幕100中定义控制控件的名称
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.
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.
ENDIF.
ENDMODULE.
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'CANCEL'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE.
输出结果:
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.
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.
输出界面如下图,双击时,注意状态条显示的信息:学校名称、学校地址的值。
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.
输出结果:
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.