(轉)SAP ABAP/4學習--BCALV_TREE_01分析
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
*----------------------------------------------------------------------*
* -->P_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
*----------------------------------------------------------------------*
* --> 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
*----------------------------------------------------------------------*
* -->P_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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SAP ABAP/4學習---BDC批次輸入寫成內表程式
- SAP ABAP 常用FUNCTION集錦(轉)Function
- SAP BASIS學習筆記(轉)筆記
- R/3 ABAP開發學習筆記(轉)筆記
- [轉]ABAP Memory/SAP Memory/Shared Buffer/DatabaseDatabase
- abap學習筆記-SAP欄位與表的對應關係筆記
- SAP ABAP呼叫WEBAPI(一)WebAPI
- SAP MM 模組的入門者,想學習 ABAP 程式語言應該如何入手?
- SAP ABAP Netweaver 裡的 ABAP 會話概念會話
- sap線上學習
- SAP CRM WebClient UI和ABAP Webdynpro頁面的互相跳轉WebclientUI
- ABAP初學者如何系統地學習ABAP程式設計?程式設計
- 轉:SAP學習筆記(from 神話blog)筆記
- SAP ABAP 基礎知識
- sap abap好用的函式函式
- 如何使用 ABAP 程式消費 SAP ABAP OData 服務
- XML初學進階學習筆記(4)(轉)XML筆記
- SAP 學習日記
- ABAP程式示例4
- SAP 後端開發的初學者,除了掌握 ABAP 之外,還需要學習什麼知識?後端
- SAP ABAP Gateway Client 的 ABAP 實現,重用 HTTP ConnectionGatewayclientHTTP
- 作為一個SAP開發人員,需要學習SAP傳統技術(比如ABAP)以外的開源技術嗎?
- SAP ABAP 程式之間的呼叫
- SAP ABAP資料表的操作
- SAP ABAP Append structure 介紹APPStruct
- SAP ABAP 效能優化實踐優化
- SAP Labs招聘Sr ABAP developerDeveloper
- SAP ABAP 解析 excel 檔案的函式 TEXT_CONVERT_XLS_TO_SAP 單步執行分析Excel函式
- 學習4
- co unixware 7.1.1 全面學習資料(4)(轉)
- SAP ABAP RFC介面通用日誌工具:abap fm logger
- SAP MDM 學習網址
- SAP MM Vendor Subrange 學習
- 學習ERP-SAP
- SAP BI 學習計劃
- SAP BW 學習筆記筆記
- 學習SAP的好地方
- SAP學習經驗談