(轉)SAP ABAP/4學習--BCALV_TREE_01分析

mmccking發表於2007-08-27
SAP ABAP/4學習--BCALV_TREE_01分析[@more@]

REPORT BCALV_TREE_01.

*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This report shows the essential steps to build up a hierarchy
* using an ALV Tree Control (class CL_GUI_ALV_TREE).
* Note that it is _not_ possible to build up this hierarchy
* using a simple ALV Tree Control (class CL_GUI_ALV_TREE_SIMPLE).
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* Start this report. The hierarchy tree consists of nodes for each
* month on top level (this level can not be build by a simple ALV Tree
* because there is no field for months in our output table SFLIGHT.
* Thus, you can not define this hierarchy by sorting).
* Nor initial calculations neither a special layout has been applied
* (the lines on the right do not show anything).
* Note also that this example does not build up and change the
* fieldcatalog of the output table. For this reason, _all_ fields
* of the output table are shown in the columns although the fields
* CARRID and FLDATE are already placed in the tree on the left.
* (Of course, this is not a good style. See BCALV_TREE_02 on how to
* hide columns).
*-------------------------------------------------------------------
* Essential steps (Search for '§')
* ~~~~~~~~~~~~~~~
* 1.Usual steps when using control technology.
* 1a. Define reference variables.
* 1b. Create ALV Tree Control and corresponding container.
* 2.Create Hierarchy-header
* 3.Create empty Tree Control
* 4.Create hierarchy (nodes and leaves)
* 4a. Select data
* 4b. Sort output table according to your conceived hierarchy
* 4c. Add data to tree
* 5.Send data to frontend.
* 6.Call dispatch to process toolbar functions
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

* §1a. Define reference variables
* 定???使用的ALV的?.和自定?的容器
DATA: G_ALV_TREE TYPE REF TO CL_GUI_ALV_TREE,
G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.

*定??出的表??
DATA: GT_SFLIGHT TYPE SFLIGHT OCCURS 0, "Output-Table
OK_CODE LIKE SY-UCOMM,
SAVE_OK LIKE SY-UCOMM, "OK-Code
G_MAX TYPE I VALUE 255.

END-OF-SELECTION.
CALL SCREEN 100.
*&---------------------------------------------------------------------*
*& Module PBO OUTPUT
*&---------------------------------------------------------------------*
* process before output
*----------------------------------------------------------------------*
MODULE PBO OUTPUT.

SET PF-STATUS 'MAIN100'.
SET TITLEBAR 'MAINTITLE'.

IF G_ALV_TREE IS INITIAL.
*初期化?個?
PERFORM INIT_TREE.
CALL METHOD CL_GUI_CFW=>FLUSH
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
IF SY-SUBRC NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
TITEL = 'Automation Queue failure'(801)
TXT1 = 'Internal error:'(802)
TXT2 = 'A method in the automation queue'(803)
TXT3 = 'caused a failure.'(804).
ENDIF.
ENDIF.
ENDMODULE. " PBO OUTPUT
*&---------------------------------------------------------------------*
*& Module PAI INPUT
*&---------------------------------------------------------------------*
* process after input
*----------------------------------------------------------------------*
MODULE PAI INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'EXIT' OR 'BACK' OR 'CANC'.
PERFORM EXIT_PROGRAM.
WHEN OTHERS.
* §6. Call dispatch to process toolbar functions
CALL METHOD CL_GUI_CFW=>DISPATCH.
ENDCASE.

CALL METHOD CL_GUI_CFW=>FLUSH.

ENDMODULE. " PAI INPUT

*&---------------------------------------------------------------------*
*& Form init_tree
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM INIT_TREE.
* §1b. Create ALV Tree Control and corresponding Container.
* create container for alv-tree
DATA: L_TREE_CONTAINER_NAME(30) TYPE C.
L_TREE_CONTAINER_NAME = 'CCONTAINER1'. "?個已?在SCREEN1000中定??了
CREATE OBJECT G_CUSTOM_CONTAINER
EXPORTING
CONTAINER_NAME = L_TREE_CONTAINER_NAME
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5.
IF SY-SUBRC <> 0.
MESSAGE X208(00) WITH 'ERROR'(100).
ENDIF.
* create tree control
CREATE OBJECT G_ALV_TREE
EXPORTING
PARENT = G_CUSTOM_CONTAINER
NODE_SELECTION_MODE = CL_GUI_COLUMN_TREE=>NODE_SEL_MODE_SINGLE
ITEM_SELECTION = 'X'
NO_HTML_HEADER = 'X'
NO_TOOLBAR = ''
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
ILLEGAL_NODE_SELECTION_MODE = 5
FAILED = 6
ILLEGAL_COLUMN_NAME = 7.
IF SY-SUBRC <> 0.
MESSAGE X208(00) WITH 'ERROR'. "#EC NOTEXT
ENDIF.
* §2. Create Hierarchy-header
* The simple ALV Tree uses the text of the fields which were used
* for sorting to define this header. When you use
* the 'normal' ALV Tree the hierarchy is build up freely
* by the programmer this is not possible, so he has to define it
* himself.
DATA L_HIERARCHY_HEADER TYPE TREEV_HHDR.
PERFORM BUILD_HIERARCHY_HEADER CHANGING L_HIERARCHY_HEADER.

* §3. Create empty Tree Control
* IMPORTANT: Table 'gt_sflight' must be empty. Do not change this table
* (even after this method call). You can change data of your table
* by calling methods of CL_GUI_ALV_TREE.
* Furthermore, the output table 'gt_outtab' must be global and can
* only be used for one ALV Tree Control.
CALL METHOD G_ALV_TREE->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'SFLIGHT'
IS_HIERARCHY_HEADER = L_HIERARCHY_HEADER
CHANGING
IT_OUTTAB = GT_SFLIGHT. "table must be empty !

* §4. Create hierarchy (nodes and leaves)
PERFORM CREATE_HIERARCHY.

* §5. Send data to frontend.
CALL METHOD G_ALV_TREE->FRONTEND_UPDATE.

* wait for automatic flush at end of pbo
ENDFORM. " init_tree
*&---------------------------------------------------------------------*
*& Form build_hierarchy_header
*&---------------------------------------------------------------------*
* build hierarchy-header-information
*----------------------------------------------------------------------*
* --&gtP_L_HIERARCHY_HEADER strucxture for hierarchy-header
*----------------------------------------------------------------------*
FORM BUILD_HIERARCHY_HEADER CHANGING
P_HIERARCHY_HEADER TYPE TREEV_HHDR.

*建立TREE的??,下面是一些屬性
P_HIERARCHY_HEADER-HEADING = 'Month/Carrier/Date'(300).
P_HIERARCHY_HEADER-TOOLTIP = 'Flights in a month'(400).
P_HIERARCHY_HEADER-WIDTH = 30.
P_HIERARCHY_HEADER-WIDTH_PIX = ' '.

ENDFORM. " build_hierarchy_header
*&---------------------------------------------------------------------*
*& Form exit_program
*&---------------------------------------------------------------------*
* free object and leave program
*----------------------------------------------------------------------*
FORM EXIT_PROGRAM.

CALL METHOD G_CUSTOM_CONTAINER->FREE.
LEAVE PROGRAM.

ENDFORM. " exit_program
*&---------------------------------------------------------------------*
*& Form create_hierarchy
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*
FORM CREATE_HIERARCHY.

DATA: LS_SFLIGHT TYPE SFLIGHT,
LT_SFLIGHT TYPE SFLIGHT OCCURS 0,
L_YYYYMM(6) TYPE C, "year and month of sflight-fldate
L_YYYYMM_LAST(6) TYPE C,
L_CARRID LIKE SFLIGHT-CARRID,
L_CARRID_LAST LIKE SFLIGHT-CARRID.

DATA: L_MONTH_KEY TYPE LVC_NKEY,
L_CARRID_KEY TYPE LVC_NKEY,
L_LAST_KEY TYPE LVC_NKEY.

* §4a. Select data
SELECT * FROM SFLIGHT INTO TABLE LT_SFLIGHT UP TO G_MAX ROWS.

* §4b. Sort output table according to your conceived hierarchy
* We sort in this order:
* year and month (top level nodes, yyyymm of DATS)
* carrier id (next level)
* day of month (leaves, dd of DATS)
SORT LT_SFLIGHT BY FLDATE+0(6) CARRID FLDATE+6(2).
* Note: The top level nodes do not correspond to a field of the
* output table. Instead we use data of the table to invent another
* hierarchy level above the levels that can be build by sorting.

* §4c. Add data to tree
LOOP AT LT_SFLIGHT INTO LS_SFLIGHT.
* Prerequesite: The table is sorted.
* You add a node everytime the values of a sorted field changes.
* Finally, the complete line is added as a leaf below the last
* node.
L_YYYYMM = LS_SFLIGHT-FLDATE+0(6).
L_CARRID = LS_SFLIGHT-CARRID.
* Top level nodes:
IF L_YYYYMM <> L_YYYYMM_LAST. "on change of l_yyyymm
L_YYYYMM_LAST = L_YYYYMM.
*Providing no key means that the node is added on top level:
PERFORM ADD_MONTH USING L_YYYYMM
''
CHANGING L_MONTH_KEY.
* The month changed, thus, there is no predecessor carrier
CLEAR L_CARRID_LAST.
ENDIF.

* Carrier nodes:
* (always inserted as child of the last month
* which is identified by 'l_month_key')
IF L_CARRID <> L_CARRID_LAST. "on change of l_carrid
L_CARRID_LAST = L_CARRID.
PERFORM ADD_CARRID_LINE USING LS_SFLIGHT
L_MONTH_KEY
CHANGING L_CARRID_KEY.
ENDIF.
* Leaf:
* (always inserted as child of the last carrier
* which is identified by 'l_carrid_key')
PERFORM ADD_COMPLETE_LINE USING LS_SFLIGHT
L_CARRID_KEY
CHANGING L_LAST_KEY.
ENDLOOP.
ENDFORM. " create_hierarchy

*&---------------------------------------------------------------------*
*& Form add_month
* ?裡?始增加月?的的?點
*&---------------------------------------------------------------------*
FORM ADD_MONTH USING P_YYYYMM TYPE C
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.
DATA: L_NODE_TEXT TYPE LVC_VALUE,
LS_SFLIGHT TYPE SFLIGHT,
L_MONTH(15) TYPE C. "output string for month

* get month name for node text
PERFORM GET_MONTH USING P_YYYYMM
CHANGING L_MONTH.
L_NODE_TEXT = L_MONTH.
* add node:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
CALL METHOD G_ALV_TREE->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = P_RELAT_KEY
I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
I_NODE_TEXT = L_NODE_TEXT
IS_OUTTAB_LINE = LS_SFLIGHT
IMPORTING
E_NEW_NODE_KEY = P_NODE_KEY.

ENDFORM. " add_month

*--------------------------------------------------------------------
FORM ADD_CARRID_LINE USING PS_SFLIGHT TYPE SFLIGHT
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.
DATA: L_NODE_TEXT TYPE LVC_VALUE,
LS_SFLIGHT TYPE SFLIGHT.
* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
L_NODE_TEXT = PS_SFLIGHT-CARRID.
CALL METHOD G_ALV_TREE->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = P_RELAT_KEY
I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
I_NODE_TEXT = L_NODE_TEXT
IS_OUTTAB_LINE = LS_SFLIGHT
IMPORTING
E_NEW_NODE_KEY = P_NODE_KEY.

ENDFORM. " add_carrid_line
*&---------------------------------------------------------------------*
*& Form add_complete_line
*&---------------------------------------------------------------------*
FORM ADD_COMPLETE_LINE USING PS_SFLIGHT TYPE SFLIGHT
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.

DATA: L_NODE_TEXT TYPE LVC_VALUE.
WRITE PS_SFLIGHT-FLDATE TO L_NODE_TEXT MM/DD/YYYY.
* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
CALL METHOD G_ALV_TREE->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = P_RELAT_KEY
I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
IS_OUTTAB_LINE = PS_SFLIGHT
I_NODE_TEXT = L_NODE_TEXT
IMPORTING
E_NEW_NODE_KEY = P_NODE_KEY.

ENDFORM. " add_complete_line
*&---------------------------------------------------------------------*
*& Form GET_MONTH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gtP_P_YYYYMM text
* *----------------------------------------------------------------------*
FORM GET_MONTH USING P_YYYYMM
CHANGING P_MONTH.
* Returns the name of month according to the digits in p_yyyymm
DATA: L_MONTHDIGITS(2) TYPE C.
L_MONTHDIGITS = P_YYYYMM+4(2).
CASE L_MONTHDIGITS.
WHEN '01'.
P_MONTH = 'January'(701).
WHEN '02'.
P_MONTH = 'February'(702).
WHEN '03'.
P_MONTH = 'March'(703).
WHEN '04'.
P_MONTH = 'April'(704).
WHEN '05'.
P_MONTH = 'May'(705).
WHEN '06'.
P_MONTH = 'June'(706).
WHEN '07'.
P_MONTH = 'July'(707).
WHEN '08'.
P_MONTH = 'August'(708).
WHEN '09'.
P_MONTH = 'September'(709).
WHEN '10'.
P_MONTH = 'October'(710).
WHEN '11'.
P_MONTH = 'November'(711).
WHEN '12'.
P_MONTH = 'December'(712).
ENDCASE.
CONCATENATE P_YYYYMM+0(4) '->' P_MONTH INTO P_MONTH.

ENDFORM. " GET_MONTH

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8356764/viewspace-966370/,如需轉載,請註明出處,否則將追究法律責任。

相關文章