OO ALV即面向对象ALV,是在屏幕上显示的一种可以自定义大小的ALV。它区别普通ALV的优点是可以多个ALV出现在同一个屏幕,也可以同其他屏幕元素同时出现在同一个屏幕。
示例展示:
1. 进入实用程序-->显示对象列表:
2.点击创建屏幕9000:
3. 开始画第一个屏幕:
4.画图如下:
部分字段属性:
5. 记得此处输入OK_CODE用于接受屏幕返回值:
6.启用9000 OUTPUT INPUT.
7.双击MODULE STATUS_9000.设置屏幕为A1,激活显示屏幕状态栏,点击创建-->GUI状态:
8.双击MODULE USER_COMMAND_9000.对屏幕状态进行响应:
DATA: ok_code(3).
MODULE USER_COMMAND_9000 INPUT.
DATA lv_ok(3).
lv_ok = ok_code.
CASE lv_ok.
WHEN 'YES'.
CALL SCREEN 9001.
WHEN '&F1'.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
9.制作OO ALV屏幕9001用于装载数据:
10. 设置MODULE STATUS_9001.MODULE USER_COMMAND_9001.如下:
MODULE STATUS_9001 OUTPUT.
SET PF-STATUS 'A2'.
ENDMODULE.
MODULE USER_COMMAND_9001 INPUT.
lv_ok = ok_code.
CASE lv_ok.
WHEN 'YES' OR '&F1'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
11.添加MODULE INIT_ALV.处理OO ALV数据:
12.源代码如下(因此示例只是自己学习所用,代码仍有改进之处):
*&---------------------------------------------------------------------*
*& Report ZTEST004
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST004.
*======================================================================*
* 说明书编号 :
* 程序名称 : ZSDR16
* 程序描述 :
* 功能顾问 : XIONGHN
* 开发顾问 :
* 开始日期 :
* 完成日期 :
*======================================================================*
* 修改历史.
*----------------------------------------------------------------------*
*
*序号. 修改人 日期 描述
*--- ----------- ---- -----------
*
*
*======================================================================*
* Tables used in the program
*TABLES: likp , lips , zsdf04_1.
*======================================================================*
*======================================================================*
* Type declarations in the program
*======================================================================*
*TYPE-POOLS: SLIS.
TYPES: BEGIN OF ty_alv,
vkorg TYPE vbak-vkorg,
tl_netwr TYPE vbap-netwr,
waerk TYPE vbap-waerk,
year TYPE char4,
month TYPE char2,
END OF ty_alv.
TYPES: BEGIN OF ty_vbakpa,
vkorg TYPE vbak-vkorg,
erdat TYPE vbak-erdat,
netwr TYPE vbap-netwr,
waerk TYPE vbap-waerk,
END OF ty_vbakpa.
TYPES: BEGIN OF ty_vbakpb,
vkorg TYPE vbak-vkorg,
erdat TYPE vbak-erdat,
netwr TYPE vbap-netwr,
waerk TYPE vbap-waerk,
END OF ty_vbakpb.
*======================================================================*
* Internal Tables used in the program
*======================================================================*
DATA : gt_fcat TYPE TABLE OF lvc_s_fcat,
gs_fcat TYPE lvc_s_fcat,
gs_layo TYPE lvc_s_layo.
DATA: go_alva TYPE REF TO cl_gui_alv_grid,
go_alvb TYPE REF TO cl_gui_alv_grid,
go_cona TYPE REF TO cl_gui_custom_container,
go_conb TYPE REF TO cl_gui_custom_container.
DATA: gt_dataa TYPE TABLE OF ty_alv,
gs_dataa TYPE ty_alv.
DATA: gt_datab TYPE TABLE OF ty_alv,
gs_datab TYPE ty_alv.
DATA: gt_vbakpa TYPE TABLE OF ty_vbakpa,
gs_vbakpa TYPE ty_vbakpa.
DATA: gt_vbakpb TYPE TABLE OF ty_vbakpb,
gs_vbakpb TYPE ty_vbakpb.
DATA: gt_waerka TYPE vbap-waerk,
gt_waerkb TYPE vbap-waerk.
DATA: gv_vkorga TYPE vbak-vkorg,
gv_vkorgb TYPE vbak-vkorg,
gv_yeara TYPE char4,
gv_yearb TYPE char4,
gv_montha TYPE char2,
gv_monthb TYPE char2.
DATA: ok_code(3).
*======================================================================*
* Data declarations in the program
*======================================================================*
*======================================================================*
* CONSTANTS
*======================================================================*
*======================================================================*
* Input Selection Screen
*======================================================================*
*======================================================================*
* Initialization
*======================================================================*
INITIALIZATION.
*======================================================================*
* At Selection Screen
*======================================================================*
AT SELECTION-SCREEN.
*======================================================================*
* AT SELECTION-SCREEN OUTPUT
*======================================================================*
AT SELECTION-SCREEN OUTPUT .
*======================================================================*
* AT SELECTION-SCREEN ON VALUE-REQUEST
*======================================================================*
*======================================================================*
* Start of Selection
*======================================================================*
START-OF-SELECTION.
CALL SCREEN 9000.
*======================================================================*
* End of Selection
*======================================================================*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form GT_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GT_DATA .
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbakpa
FROM vbak
JOIN vbap ON vbak~vbeln = vbap~vbeln
WHERE vkorg = gv_vkorga.
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbakpb
FROM vbak
JOIN vbap ON vbak~vbeln = vbap~vbeln
WHERE vbak~vkorg = gv_vkorgb.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form HDL_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM HDL_DATA .
CLEAR gt_dataa.
CLEAR gt_datab.
SELECT DISTINCT vbap~waerk
FROM vbap
JOIN vbak ON vbap~vbeln = vbak~vbeln
INTO gt_waerka WHERE vbak~vkorg = gv_vkorga.
LOOP AT gt_vbakpa INTO gs_vbakpa
WHERE waerk = gt_waerka
AND erdat+0(4) = gv_yeara
AND erdat+4(2) = gv_montha.
gs_dataa-vkorg = gs_vbakpa-vkorg.
gs_dataa-waerk = gt_waerka.
gs_dataa-year = gv_yeara.
gs_dataa-month = gv_montha.
IF sy-subrc = 0.
gs_dataa-tl_netwr = gs_vbakpa-netwr + gs_dataa-tl_netwr.
ENDIF.
ENDLOOP.
IF sy-subrc = 0.
APPEND gs_dataa TO gt_dataa.
CLEAR gs_dataa.
CLEAR gs_vbakpa.
ENDIF.
ENDSELECT.
SELECT DISTINCT vbap~waerk
FROM vbap
JOIN vbak ON vbap~vbeln = vbak~vbeln
INTO gt_waerkb WHERE vbak~vkorg = gv_vkorgb.
LOOP AT gt_vbakpb INTO gs_vbakpb
WHERE waerk = gt_waerkb
AND erdat+0(4) = gv_yearb
AND erdat+4(2) = gv_monthb.
gs_datab-vkorg = gs_vbakpb-vkorg.
gs_datab-waerk = gt_waerkb.
gs_datab-year = gv_yearb.
gs_datab-month = gv_monthb.
IF sy-subrc = 0.
gs_datab-tl_netwr = gs_vbakpb-netwr + gs_datab-tl_netwr.
ENDIF.
ENDLOOP.
IF sy-subrc = 0.
APPEND gs_datab TO gt_datab.
CLEAR gs_datab.
CLEAR gs_vbakpb.
ENDIF.
ENDSELECT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ALV_SHOW
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM ALV_SHOW .
PERFORM init_layout.
PERFORM frm_set_fieldset.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INIT_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM INIT_LAYOUT .
CLEAR gs_layo.
gs_layo-cwidth_opt = 'X'.
gs_layo-zebra = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDSET
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_FIELDSET .
PERFORM frm_init_fieldcat USING 'VKORG' '销售组织' 'X' 10.
PERFORM frm_init_fieldcat USING 'TL_NETWR' '销售额' space 10.
PERFORM frm_init_fieldcat USING 'WAERK' '销售币别' space 6.
PERFORM frm_init_fieldcat USING 'YEAR' '年份' space 6.
PERFORM frm_init_fieldcat USING 'MONTH' '月份' space 4.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_INIT_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0396 text
* -->P_0397 text
* -->P_SPACE text
* -->P_10 text
*----------------------------------------------------------------------*
FORM FRM_INIT_FIELDCAT USING VALUE(p_fieldname) TYPE lvc_fname
VALUE(p_scrtext) TYPE scrtext_l
VALUE(p_key) TYPE lvc_key
VALUE(p_len) TYPE lvc_outlen.
gs_fcat
-fieldname = p_fieldname.
gs_fcat
-scrtext_l = p_scrtext.
gs_fcat
-key = p_key.
gs_fcat
-outputlen = p_len.
APPEND gs_fcat to gt_fcat.
CLEAR gs_fcat.
ENDFORM.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_9000 OUTPUT.
SET PF-STATUS 'A1'.
* SET TITLEBAR 'xxx'.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module STATUS_9001 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_9001 OUTPUT.
SET PF-STATUS 'A2'.
* SET TITLEBAR 'xxx'.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module INIT_ALV OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE INIT_ALV OUTPUT.
CLEAR gt_fcat.
PERFORM con_free.
PERFORM gt_data.
PERFORM hdl_data.
IF gt_dataa IS INITIAL AND gt_datab IS INITIAL.
MESSAGE '没有符合的数据,请重新查询' TYPE 'S' DISPLAY LIKE 'E'.
ELSE.
PERFORM alv_show.
ENDIF.
CREATE OBJECT go_cona
EXPORTING
container_name
= 'CONA'.
CREATE OBJECT go_alva
EXPORTING
i_parent
= go_cona.
CALL METHOD go_alva->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT
= gs_layo
CHANGING
IT_OUTTAB
= gt_dataa
IT_FIELDCATALOG
= gt_fcat
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
INVALID_PARAMETER_COMBINATION
= 1
PROGRAM_ERROR
= 2
TOO_MANY_LINES
= 3
others = 4
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
IF go_conb IS INITIAL.
CREATE OBJECT go_conb
EXPORTING
container_name
= 'CONB'.
CREATE OBJECT go_alvb
EXPORTING
i_parent
= go_conb.
CALL METHOD go_alvb->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT
= gs_layo
CHANGING
IT_OUTTAB
= gt_datab
IT_FIELDCATALOG
= gt_fcat
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
INVALID_PARAMETER_COMBINATION
= 1
PROGRAM_ERROR
= 2
TOO_MANY_LINES
= 3
others = 4
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ELSE.
CALL METHOD go_alvb->REFRESH_TABLE_DISPLAY.
ENDIF.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9000 INPUT.
DATA lv_ok(3).
lv_ok
= ok_code.
CASE lv_ok.
WHEN 'YES'.
CALL SCREEN 9001.
WHEN '&F1'.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9001 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9001 INPUT.
lv_ok
= ok_code.
* CLEAR ok_code.
CASE lv_ok.
WHEN 'YES' OR '&F1'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Form CON_FREE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CON_FREE .
IF go_alva IS NOT INITIAL.
CALL METHOD go_alva->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.
ELSE.
FREE go_alva.
ENDIF.
IF go_cona IS NOT INITIAL.
CALL METHOD go_cona->free
EXCEPTIONS
CNTL_ERROR
= 1
CNTL_SYSTEM_ERROR
= 2
OTHERS = 3.
IF SY-SUBRC NE 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
FREE go_cona.
ENDIF.
ENDIF.
ENDFORM.