在这一篇中将学习一下ALV Tree的使用方法。通过名字可以看出,ALV TREE应该是将ALV与TREE这两者进行组合。
ALV Tree实现原理:创建一个 cl_gui_alv_tree对象,定义一个 treev_hhdr类型的数据,主要用来显示tree的Header信息。调用ALVtree的 SET_TABLE_FOR_FIRST_DISPLAY方法,调用ALV tree的 ADD_NODE方法逐行添加数据。在这里不进行详细描述,直接看代码吧。
*&---------------------------------------------------------------------*
*& Report ZTEST_TREE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_TREE.
DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree,
g_customer_container TYPE REF TO cl_gui_custom_container.
DATA: gt_sflight type sflight occurs 0,
ok_code like sy-ucomm,
save_ok like sy-ucomm.
start-of-SELECTION.
set SCREEN 100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'MAIN0100'.
* SET TITLEBAR 'xxx'.
IF g_alv_tree is INITIAL.
PERFORM init_tree.
ENDIF.
call method cl_gui_cfw=>flush.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
save_ok = ok_code .
CLEAR ok_code.
CASE save_ok .
WHEN 'EXIT'.
CALL METHOD g_alv_tree->FREE.
leave program.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form init_tree
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM init_tree .
CREATE OBJECT g_customer_container
EXPORTING
CONTAINER_NAME = 'CUS_CON'
.
CREATE OBJECT g_alv_tree
EXPORTING
PARENT = g_customer_container
NODE_SELECTION_MODE = CL_GUI_COLUMN_TREE=>node_sel_mode_single
ITEM_SELECTION = 'X'
NO_HTML_HEADER = 'X'
.
DATA: g_tree_header type treev_hhdr.
g_tree_header-HEADING = 'Month/Carrier/Date'.
g_tree_header-TOOLTIP = 'Flights in a month'.
g_tree_header-width = 30.
CALL METHOD g_alv_tree->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'SFLIGHT'
IS_HIERARCHY_HEADER = g_tree_header
CHANGING
IT_OUTTAB = gt_sflight.
perform create_hierarchy.
CALL METHOD g_alv_tree->FRONTEND_UPDATE.
ENDFORM. " init_tree
*&---------------------------------------------------------------------*
*& Form create_hierarchy
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_hierarchy .
DATA: ls_sflight type sflight,
lt_sflight type sflight occurs 0,
t_yyyymm(6) TYPE C,
t_carrid LIKE sflight-carrid,
t_yyyymm_last(6) TYPE C,
t_carrid_last LIKE sflight-carrid.
DATA: K_month type lvc_nkey,
K_carrid type lvc_nkey,
K_last TYPE lvc_nkey.
select * from sflight into table lt_sflight up to 150 rows.
SORT lt_sflight by fldate+0(6) carrid fldate+6(2).
LOOP AT lt_sflight INTO ls_sflight.
t_yyyymm = ls_sflight-fldate+0(6).
t_carrid = ls_sflight-carrid.
IF t_yyyymm <> t_yyyymm_last.
t_yyyymm_last = t_yyyymm.
PERFORM AddMonth USING t_yyyymm
CHANGING K_month .
CLEAR t_carrid_last.
ENDIF.
IF t_carrid <> t_carrid_last.
t_carrid_last = t_carrid.
PERFORM AddCarrid USING t_carrid K_month
CHANGING K_carrid.
ENDIF.
PERFORM AddLine USING ls_sflight K_carrid
CHANGING K_last.
ENDLOOP.
ENDFORM. " create_hierarchy
*&---------------------------------------------------------------------*
*& Form AddMonth
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_T_YYYYMM text
* <--P_K_MONTH text
*----------------------------------------------------------------------*
FORM AddMonth USING P_T_YYYYMM TYPE C
CHANGING P_K_MONTH TYPE lvc_nkey.
DATA: g_sflight TYPE sflight,
l_node_text type lvc_value.
l_node_text = P_T_YYYYMM.
CALL METHOD g_alv_tree->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = ''
I_RELATIONSHIP = cl_gui_column_tree=>relat_last_child
IS_OUTTAB_LINE = g_sflight
* IS_NODE_LAYOUT =
* IT_ITEM_LAYOUT =
I_NODE_TEXT = l_node_text
IMPORTING
E_NEW_NODE_KEY = P_K_MONTH
* EXCEPTIONS
* RELAT_NODE_NOT_FOUND = 1
* NODE_NOT_FOUND = 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.
ENDFORM. " AddMonth
*&---------------------------------------------------------------------*
*& Form AddCarrid
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_T_CARRID text
* -->P_K_MONTH text
* <--P_K_CARRID text
*----------------------------------------------------------------------*
FORM AddCarrid USING P_T_CARRID TYPE sflight-carrid
P_K_MONTH TYPE lvc_nkey
CHANGING P_K_CARRID TYPE lvc_nkey.
DATA: g_sflight TYPE sflight,
l_node_text type lvc_value.
l_node_text = P_T_CARRID.
CALL METHOD g_alv_tree->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = P_K_MONTH
I_RELATIONSHIP = cl_gui_column_tree=>relat_last_child
IS_OUTTAB_LINE = g_sflight
* IS_NODE_LAYOUT =
* IT_ITEM_LAYOUT =
I_NODE_TEXT = l_node_text
IMPORTING
E_NEW_NODE_KEY = P_K_CARRID
* EXCEPTIONS
* RELAT_NODE_NOT_FOUND = 1
* NODE_NOT_FOUND = 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.
ENDFORM. " AddCarrid
*&---------------------------------------------------------------------*
*& Form AddLine
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LS_SFLIGHT text
* -->P_K_CARRID text
* <--P_K_LAST text
*----------------------------------------------------------------------*
FORM AddLine USING P_LS_SFLIGHT TYPE sflight
P_K_CARRID TYPE lvc_nkey
CHANGING P_K_LAST TYPE lvc_nkey.
DATA: l_node_text type lvc_value.
l_node_text = P_LS_SFLIGHT-fldate.
CALL METHOD g_alv_tree->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = P_K_CARRID
I_RELATIONSHIP = cl_gui_column_tree=>relat_last_child
IS_OUTTAB_LINE = P_LS_SFLIGHT
* IS_NODE_LAYOUT =
* IT_ITEM_LAYOUT =
I_NODE_TEXT = l_node_text
IMPORTING
E_NEW_NODE_KEY = P_K_LAST
* EXCEPTIONS
* RELAT_NODE_NOT_FOUND = 1
* NODE_NOT_FOUND = 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.
ENDFORM. " AddLine