SAP ABAP/4学习--BCALV_TREE_01分析

此报告展示了如何使用ABAP/4中的BCALV_TREE_01创建一个层次结构,重点是使用ALV Tree Control(CL_GUI_ALV_TREE类)构建层次结构。示例中,通过创建层级头、空树控制,以及添加节点和叶子,展示了一个基于月份的层级树。程序不包括初始化计算和特殊布局,所有输出表字段都显示在列中,尽管部分字段已放置在树上。

BCALV_TREE_01分析

                                SSOLE

 

 

 

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

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        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

*  <--  p2        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

*      <--P_L_MONTH  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

*-----------------------------------------------------------------------

 

 

 

 

Example program

Theme

SAPSIMPLE_TREE_CONTROL_DEMO

Example of a simple tree

SAPTLIST_TREE_CONTROL_DEMO

Example of a list tree

SAPCOLUMN_TREE_CONTROL_DEMO

Example of a column tree

SAPSIMPLE_TREE_CONTEXT_MEN_DEM

Example of context menus

SAPTLIST_TREE_CONTROL_DEMO_HDR

Example of a context menu on headings in a SAP Tree

SAPSIMPLE_TREE_DRAG_DROP_DEMO

Example of drag and drop

RSDEMO_DRAG_DROP_TREE_MULTI

Example of drag and drop with multiple selection

RSDEMO_DRAG_DROP_EDIT_TREE

Example of drag and drop between a SAP Tree and a SAP Textedit

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值