ABAP Tree Control的使用

Tree Control是最常用的Windows控件之一,在其他语言中,称为“Tree View”等,ABAP的Tree Control能完成相似的功能,本章主要介绍以下实例:
(1)实现一个简单的Tree Control;
(2)建立有数据表接口的Tree Control对象;
(3)通过右键菜单完成Tree Control节点的增删修改等功能;
(4)Tree Control对象与ALV Grid对象关联,实现在双击Tree节点时,变更ALV Grid的显示。

1、简单的Tree Control实例

本例将实现以下功能:
(1)建立一个以国家、城市、城区三层的Tree Control对象(固定数据);

(2)当双击某个节点时,在文本框显示选中节点的KEY值和文本值。


实现步骤如下(前两步为基本功能,不再详细描述):
(1)建立一个ABAP程序;
(2)使用屏幕制作器,建立 Screen。
A、添加“定制控制”对象;
B、添加两个文本框,存放节点KEY值和节点文本值。
(3)建立程序
A、处理窗口调用,退出等基本功能;
B、创建窗口定制对象,Tree Control对象,测试运行时能看到TREE区域
C、实现Tree节点的创建,测试运行,能看到Tree的所有节点;
D、定义对象,建立双击方法,并将双击方法分配Tree对象,实现双击事件。

建立好的屏幕和对象界面如图:

ABAP Tree Control的使用_第1张图片

元素清单如图:

逻辑流程序:

PROCESS BEFORE OUTPUT.
 MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0100.
主程序:

REPORT  Z_TREETEST_20160629001.
*功能码返回值
DATA: OK_CODE TYPE SY-UCOMM,
      SAVE_OK TYPE SY-UCOMM.
*定制对象
DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE',
*客户对象
      WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
*NODE节点表
DATA: NODE_TABLE LIKE TABLE OF MTREESNODE,
*节点
      NODE1 TYPE MTREESNODE.
*定义内表,变量需要传递,不加HEADER LINE
DATA WA_SPFLI TYPE TABLE OF SPFLI.
*当双击时取得节点KEY值与节点文本值变量
DATA: NODEKEY(200) VALUE 'NODE', NODETEXT(200) VALUE 'NODE'.
*定义对象,定义对象的双击方法,用于将该对象双击方法分配到树双击事件
CLASS LCL_APPLICATION DEFINITION DEFERRED.

CLASS LCL_APPLICATION DEFINITION.
  PUBLIC SECTION.
*自定义双击方法,参数为节点关键字
    METHODS HANDLE_NODE_DOUBLE_CLICK
      FOR EVENT NODE_DOUBLE_CLICK
      OF CL_GUI_SIMPLE_TREE
      IMPORTING NODE_KEY.
ENDCLASS.

CLASS LCL_APPLICATION IMPLEMENTATION.
  METHOD HANDLE_NODE_DOUBLE_CLICK.
    NODEKEY = NODE_KEY.
    READ TABLE NODE_TABLE WITH KEY NODE_KEY = NODE_KEY INTO NODE1.
    NODETEXT = NODE1-TEXT.
  ENDMETHOD.
ENDCLASS.

*自定义对象实例
DATA: G_APPLICATION TYPE REF TO LCL_APPLICATION.
START-OF-SELECTION.
  CREATE OBJECT G_APPLICATION.
  CALL SCREEN 100.

MODULE STATUS_0100 OUTPUT.
  IF WA_CUSTOM_CONTAINER IS INITIAL.
    PERFORM CREATE_TREE.
  ENDIF.
ENDMODULE.

MODULE USER_COMMAND_0100 INPUT.
  SAVE_OK = OK_CODE.
  CLEAR OK_CODE.
  CASE SAVE_OK.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.

FORM CREATE_TREE.
*事件内表及单个事件对象.
  DATA: EVENTS TYPE CNTL_SIMPLE_EVENTS,
        EVENT1 TYPE CNTL_SIMPLE_EVENT.
*建立定制控制对象
  CREATE OBJECT WA_CUSTOM_CONTAINER
    EXPORTING
      CONTAINER_NAME = WA_CONTAINER.
*建立树对象
  CREATE OBJECT WA_TREE
    EXPORTING
      PARENT = WA_CUSTOM_CONTAINER
      NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE.

  IF SY-SUBRC <> 0.
  ENDIF.
*开始建立以国家->城市->城区 三层的树
  CLEAR NODE1.
*节点关键字
  NODE1-NODE_KEY = 'CHINA'.
*第一层
  CLEAR NODE1-RELATKEY.
  CLEAR NODE1-RELATSHIP.
*是文件夹
  NODE1-HIDDEN = ''.
  NODE1-DISABLED = ''.
  NODE1-ISFOLDER = 'X'.
*不带ICON
  CLEAR NODE1-N_IMAGE.
  CLEAR NODE1-EXP_IMAGE.
  CLEAR NODE1-EXPANDER.

  NODE1-TEXT = '中国'.
  APPEND NODE1 TO NODE_TABLE.


*二层节点
  CLEAR NODE1.
  NODE1-NODE_KEY = 'BJ'.
*上层关系
  NODE1-RELATKEY = 'CHINA'.
  NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  NODE1-HIDDEN = ''.
  NODE1-DISABLED = ''.
  NODE1-ISFOLDER = 'X'.
  CLEAR NODE1-N_IMAGE.
  CLEAR NODE1-EXP_IMAGE.
  CLEAR NODE1-EXPANDER.
  NODE1-TEXT = '北京'.
  APPEND NODE1 TO NODE_TABLE.

  CLEAR NODE1.
  NODE1-NODE_KEY = 'SH'.
  NODE1-RELATKEY = 'CHINA'.
  NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  NODE1-HIDDEN = ''.
  NODE1-DISABLED = ''.
  NODE1-ISFOLDER = 'X'.
  CLEAR NODE1-N_IMAGE.
  CLEAR NODE1-EXP_IMAGE.
  CLEAR NODE1-EXPANDER.
  NODE1-TEXT = '上海'.
  APPEND NODE1 TO NODE_TABLE.

*三层节点
  CLEAR NODE1.
  NODE1-NODE_KEY = 'CWQ'.
  NODE1-RELATKEY = 'BJ'.
  NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  NODE1-HIDDEN = ''.
  NODE1-DISABLED = ''.
  NODE1-ISFOLDER = ''.
*图标,可运行SHOWICON查阅
  NODE1-N_IMAGE = '@9Y@'.
  NODE1-EXP_IMAGE = '@9Y@'.
  CLEAR NODE1-EXPANDER.
  NODE1-TEXT = '崇文区'.
  APPEND NODE1 TO NODE_TABLE.

  CLEAR NODE1.
  NODE1-NODE_KEY = 'XCQ'.
  NODE1-RELATKEY = 'BJ'.
  NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  NODE1-HIDDEN = ''.
  NODE1-DISABLED = ''.
  NODE1-ISFOLDER = ''.
  NODE1-N_IMAGE = '@DF@'.
  NODE1-EXP_IMAGE = '@DF@'.
  CLEAR NODE1-EXPANDER.
  NODE1-TEXT = '西城区'.
  APPEND NODE1 TO NODE_TABLE.

  CLEAR NODE1.
  NODE1-NODE_KEY = 'MXQ'.
  NODE1-RELATKEY = 'SH'.
  NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  NODE1-HIDDEN = ''.
  NODE1-DISABLED = ''.
  NODE1-ISFOLDER = ''.
  NODE1-N_IMAGE = '@NL@'.
  NODE1-EXP_IMAGE = '@NL@'.
  CLEAR NODE1-EXPANDER.
  NODE1-TEXT = '闵行区'.
  APPEND NODE1 TO NODE_TABLE.

*按照节点内容添加节点
  CALL METHOD WA_TREE->ADD_NODES
    EXPORTING
      TABLE_STRUCTURE_NAME = 'MTREESNODE'
      NODE_TABLE = NODE_TABLE.

  IF SY-SUBRC <> 0.
  ENDIF.

*定义双击事件
  EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK.
  EVENT1-APPL_EVENT = 'X'.
  APPEND EVENT1 TO EVENTS.
*添加事件内表
  CALL METHOD WA_TREE->SET_REGISTERED_EVENTS
    EXPORTING
      EVENTS = EVENTS.

  IF SY-SUBRC <> 0.
  ENDIF.

*将已定义的双击事件分配至树对象
  SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR WA_TREE.
ENDFORM.

2、建立有数据表接口的Tree Control对象

先从数据表读取结构,并按照数据关系建立Tree Control,如图

ABAP Tree Control的使用_第2张图片

以航班表为例,以“起飞国家-->航班承运人-->航班连接”三层组成。

建立好的屏幕和对象界面如图:

ABAP Tree Control的使用_第3张图片
元素清单如图:

ABAP Tree Control的使用_第4张图片
逻辑流程序:

PROCESS BEFORE OUTPUT.
 MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0100.
主程序:

REPORT  Z_TREETEST_20160629002.
DATA: OK_CODE TYPE SY-UCOMM,
      SAVE_OK TYPE SY-UCOMM.
*定制对象
DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE',
*客户对象
      WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
*NODE节点表
DATA: NODE_TABLE LIKE TABLE OF MTREESNODE,
*节点
      NODE1 TYPE MTREESNODE.

DATA WA_SPFLI TYPE TABLE OF SPFLI WITH HEADER LINE.

START-OF-SELECTION.
  SELECT * INTO TABLE WA_SPFLI FROM SPFLI
    ORDER BY COUNTRYFR CARRID CONNID ASCENDING.

    SET SCREEN 100.

MODULE STATUS_0100 OUTPUT.
  IF WA_CUSTOM_CONTAINER IS INITIAL.
    PERFORM CREATE_TREE.
  ENDIF.
ENDMODULE.

MODULE USER_COMMAND_0100 INPUT.
  SAVE_OK = OK_CODE.
  CLEAR OK_CODE.
  CASE SAVE_OK.
    WHEN 'CANCEL'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.

FORM CREATE_TREE.
*建立定制控制对象
  CREATE OBJECT WA_CUSTOM_CONTAINER
    EXPORTING
      CONTAINER_NAME = WA_CONTAINER.
*建立树对象
  CREATE OBJECT WA_TREE
    EXPORTING
      PARENT = WA_CUSTOM_CONTAINER
      NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE.

  IF SY-SUBRC <> 0.
  ENDIF.

  DATA: COUNTRYFR LIKE SPFLI-COUNTRYFR,
        CARRID LIKE SPFLI-CARRID.

  DATA:STR1(255).

  LOOP AT WA_SPFLI.
    IF COUNTRYFR NE WA_SPFLI-COUNTRYFR.
      CLEAR NODE1.
      NODE1-NODE_KEY = WA_SPFLI-COUNTRYFR.
*第一层
      CLEAR NODE1-RELATKEY.
      CLEAR NODE1-RELATSHIP.
*是文件夹
      NODE1-HIDDEN = ''.
      NODE1-DISABLED = ''.
      NODE1-ISFOLDER = 'X'.
*不带ICON
      CLEAR NODE1-N_IMAGE.
      CLEAR NODE1-EXP_IMAGE.
      CLEAR NODE1-EXPANDER.

      NODE1-TEXT = WA_SPFLI-COUNTRYFR.
      APPEND NODE1 TO NODE_TABLE.
    ENDIF.

    IF ( COUNTRYFR NE WA_SPFLI-COUNTRYFR ) OR ( CARRID <> WA_SPFLI-CARRID ).
      CLEAR NODE1.
      CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID INTO NODE1-NODE_KEY.
*上层关系
      NODE1-RELATKEY = WA_SPFLI-COUNTRYFR.
      NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
      NODE1-HIDDEN = ''.
      NODE1-DISABLED = ''.
      NODE1-ISFOLDER = 'X'.
      CLEAR NODE1-N_IMAGE.
      CLEAR NODE1-EXP_IMAGE.
      CLEAR NODE1-EXPANDER.
*节点显示CARRID
      NODE1-TEXT = WA_SPFLI-CARRID.
      APPEND NODE1 TO NODE_TABLE.
    ENDIF.

    COUNTRYFR = WA_SPFLI-COUNTRYFR.
    CARRID = WA_SPFLI-CARRID.
    CLEAR NODE1.
    CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID
          WA_SPFLI-CONNID INTO NODE1-NODE_KEY.

    CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID INTO STR1.
    NODE1-RELATKEY = STR1.
    NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
    NODE1-HIDDEN = ''.
    NODE1-DISABLED = ''.
    NODE1-ISFOLDER = 'X'.
    NODE1-N_IMAGE = '@5B@'.
    NODE1-EXP_IMAGE = '@5B@'.
    CLEAR NODE1-EXPANDER.
    CONCATENATE WA_SPFLI-CARRID ':' WA_SPFLI-CITYFROM INTO STR1.
    NODE1-TEXT = STR1.
    APPEND NODE1 TO NODE_TABLE.
  ENDLOOP.

*按照节点内容添加节点
  CALL METHOD WA_TREE->ADD_NODES
    EXPORTING
      TABLE_STRUCTURE_NAME = 'MTREESNODE'
      NODE_TABLE = NODE_TABLE.
  IF SY-SUBRC <> 0.
  ENDIF.
ENDFORM.

3、通过右键菜单完成Tree Control节点的增删改功能

Tree节点的增删改是很重要的功能,包括:增加本级节点,增加子节点,修改节点,删除节点。完成后效果如下:

选择根节点,单击右键,弹出右键菜单,如选择“北京”节点,选择“增加本级节点”菜单,如图

ABAP Tree Control的使用_第5张图片

在单击文本输入“广州”后,单击“更新”按钮,如图


Tree对象增加了“广州”节点,选择该节点,选右键菜单中“增加子节点”菜单,如图

ABAP Tree Control的使用_第6张图片

输入”天河区“,单击”更新“按钮,如图


增加节点后,输出结果如图

ABAP Tree Control的使用_第7张图片

除上术功能外,还有修改及删除节点等功能

建立好的屏幕和对象界面如图:

ABAP Tree Control的使用_第8张图片

元素清单如图:

ABAP Tree Control的使用_第9张图片

逻辑流程序:

PROCESS BEFORE OUTPUT.
 MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0100.

主程序:

REPORT  z_treetest_20160630001.
DATA: ok_code TYPE sy-ucomm,
      save_ok TYPE sy-ucomm.

DATA: wa_container TYPE scrfname VALUE 'TREE',
      wa_custom_container TYPE REF TO cl_gui_custom_container,
      wa_tree TYPE REF TO cl_gui_simple_tree.

DATA: node_table LIKE TABLE OF mtreesnode,
      anode_table LIKE TABLE OF mtreesnode,
      node1 TYPE mtreesnode,
      node2 TYPE mtreesnode.

DATA wa_spfli TYPE TABLE OF spfli.

DATA: nodekey(200) VALUE 'NODE',
      nodetext(200) VALUE 'NODE',
      nodekeyold(200) VALUE 'NODE',
      getkey(200).

DATA menustr(10) VALUE ''.

CLASS lcl_application DEFINITION DEFERRED.

*----------------------------------------------------------------------*
*       CLASS LCL_APPLICATION DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_application DEFINITION.
  PUBLIC SECTION.
    METHODS:
    handle_node_double_click
                FOR EVENT node_double_click
                OF cl_gui_simple_tree
                IMPORTING node_key.

    METHODS:
    handle_node_context_menu_req
    FOR EVENT node_context_menu_request
    OF cl_gui_simple_tree
    IMPORTING node_key menu.

    METHODS:
          handle_node_context_menu_sel
            FOR EVENT node_context_menu_select
            OF cl_gui_simple_tree
            IMPORTING node_key fcode.
ENDCLASS.                    "LCL_APPLICATION DEFINITION

*----------------------------------------------------------------------*
*       CLASS LCL_APPLICATION IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_application IMPLEMENTATION.
  METHOD handle_node_double_click.
    nodekey = node_key.
    READ TABLE node_table WITH KEY node_key = node_key INTO node1.
    nodetext = node1-text.
  ENDMETHOD.                    "HANDLE_NODE_DOUBLE_CLICK

  METHOD handle_node_context_menu_req.
    CALL METHOD menu->add_function
      EXPORTING
        text  = '增加本级节点'
        fcode = 'CODE1'.

    CALL METHOD menu->add_function
      EXPORTING
        text  = '增加子节点'
        fcode = 'CODE2'.

    CALL METHOD menu->add_function
      EXPORTING
        text  = '修改当前节点'
        fcode = 'CODE3'.
    CALL METHOD menu->add_function
      EXPORTING
        text  = '删除当前节点'
        fcode = 'CODE4'.
  ENDMETHOD.                    "HANDLE_NODE_CONTEXT_MENU_REQ

  METHOD handle_node_context_menu_sel.
    DATA str1 TYPE tv_nodekey.
    CALL METHOD wa_tree->get_selected_node
      IMPORTING
        node_key = str1.
    IF sy-subrc = 0.
      menustr = fcode.
      nodekey = str1.
      nodekeyold = str1.

      READ TABLE node_table WITH KEY node_key = nodekey INTO node1.
      nodetext = node1-text.
      IF fcode = 'CODE4'.
        CALL METHOD wa_tree->delete_node
          EXPORTING
            node_key = node1-node_key.
        DELETE node_table WHERE node_key = nodekey.
      ENDIF.
    ENDIF.
  ENDMETHOD.                    "HANDLE_NODE_CONTEXT_MENU_SEL
ENDCLASS.                    "LCL_APPLICATION IMPLEMENTATION

DATA: g_application TYPE REF TO lcl_application.

START-OF-SELECTION.
  CALL SCREEN 100.

*----------------------------------------------------------------------*
*  MODULE STATUS_0100 OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  IF wa_custom_container IS INITIAL.
    CREATE OBJECT g_application.
    PERFORM create_tree.
  ENDIF.
ENDMODULE.                    "STATUS_0100 OUTPUT

*----------------------------------------------------------------------*
*  MODULE USER_COMMAND_0100 INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  DATA str1 TYPE tv_nodekey.
  save_ok = ok_code.
  CLEAR ok_code.

  CASE save_ok.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
    WHEN 'GETKEY'.
      CALL METHOD wa_tree->get_selected_node
        IMPORTING
          node_key = str1.

      IF sy-subrc EQ 0.
        getkey = str1.
      ENDIF.

    WHEN 'UPDATE'.
      IF ( menustr = 'CODE1' ) OR ( menustr = 'CODE2').
        READ TABLE node_table
            WITH KEY node_key = nodekey INTO node1.
        IF sy-subrc EQ 0.
          MESSAGE e208(00) WITH '键值重复!'.
        ENDIF.
      ENDIF.

      CASE menustr.
        WHEN 'CODE1'.
          READ TABLE node_table WITH KEY node_key = nodekeyold INTO node2.

          CLEAR node1.
          node1-node_key = nodekey.
          node1-relatkey = node2-relatkey.
          node1-relatship = cl_gui_simple_tree=>relat_last_child.
          node1-hidden = ''.
          node1-disabled = ''.
          node1-isfolder = 'X'.
          node1-n_image = '@G3@'.
          node1-exp_image = '@G3@'.
          CLEAR node1-expander.
          node1-text = nodetext.
          APPEND node1 TO node_table.
          CLEAR anode_table.
          APPEND node1 TO anode_table.

          CALL METHOD wa_tree->add_nodes
            EXPORTING
              table_structure_name = 'MTREESNODE'
              node_table           = anode_table.
          IF sy-subrc <> 0.
          ENDIF.

        WHEN 'CODE2'.
          CLEAR node1.
          node1-node_key = nodekey.
          node1-relatkey = nodekeyold.
          node1-relatship = cl_gui_simple_tree=>relat_last_child.
          node1-hidden = ''.
          node1-disabled = ''.
          node1-isfolder = 'X'.
          node1-n_image = '@G3@'.
          node1-exp_image = '@G3@'.
          CLEAR node1-expander.
          node1-text = nodetext.
          APPEND node1 TO node_table.
          CLEAR anode_table.
          APPEND node1 TO anode_table.

          CALL METHOD wa_tree->add_nodes
            EXPORTING
              table_structure_name = 'MTREESNODE'
              node_table           = anode_table.
          IF sy-subrc <> 0.
          ENDIF.

        WHEN 'CODE3'.
          node1-node_key = nodekeyold.
          CALL METHOD wa_tree->node_set_text
            EXPORTING
              node_key = node1-node_key
              text     = nodetext.
          IF sy-subrc <> 0.
          ENDIF.

      ENDCASE.
  ENDCASE.
ENDMODULE.                    "USER_COMMAND_0100 INPUT

*&---------------------------------------------------------------------*
*&      Form  CREATE_TREE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_tree.
*事件内表及单个事件对象.
  DATA: events TYPE cntl_simple_events,
        event1 TYPE cntl_simple_event.
*建立定制控制对象
  CREATE OBJECT wa_custom_container
    EXPORTING
      container_name = wa_container.
*建立树对象
  CREATE OBJECT wa_tree
    EXPORTING
      parent              = wa_custom_container
      node_selection_mode = cl_gui_simple_tree=>node_sel_mode_single.

  IF sy-subrc <> 0.
  ENDIF.
*开始建立以国家->城市->城区 三层的树
  CLEAR node1.
*节点关键字
  node1-node_key = 'CHINA'.
*第一层
  CLEAR node1-relatkey.
  CLEAR node1-relatship.
*是文件夹
  node1-hidden = ''.
  node1-disabled = ''.
  node1-isfolder = 'X'.
*不带ICON
  CLEAR node1-n_image.
  CLEAR node1-exp_image.
  CLEAR node1-expander.

  node1-text = '中国'.
  APPEND node1 TO node_table.


*二层节点
  CLEAR node1.
  node1-node_key = 'BJ'.
*上层关系
  node1-relatkey = 'CHINA'.
  node1-relatship = cl_gui_simple_tree=>relat_last_child.
  node1-hidden = ''.
  node1-disabled = ''.
  node1-isfolder = 'X'.
  CLEAR node1-n_image.
  CLEAR node1-exp_image.
  CLEAR node1-expander.
  node1-text = '北京'.
  APPEND node1 TO node_table.

  CLEAR node1.
  node1-node_key = 'SH'.
  node1-relatkey = 'CHINA'.
  node1-relatship = cl_gui_simple_tree=>relat_last_child.
  node1-hidden = ''.
  node1-disabled = ''.
  node1-isfolder = 'X'.
  CLEAR node1-n_image.
  CLEAR node1-exp_image.
  CLEAR node1-expander.
  node1-text = '上海'.
  APPEND node1 TO node_table.

*三层节点
  CLEAR node1.
  node1-node_key = 'CWQ'.
  node1-relatkey = 'BJ'.
  node1-relatship = cl_gui_simple_tree=>relat_last_child.
  node1-hidden = ''.
  node1-disabled = ''.
  node1-isfolder = ''.
*图标,可运行SHOWICON查阅
  node1-n_image = '@9Y@'.
  node1-exp_image = '@9Y@'.
  CLEAR node1-expander.
  node1-text = '崇文区'.
  APPEND node1 TO node_table.

  CLEAR node1.
  node1-node_key = 'XCQ'.
  node1-relatkey = 'BJ'.
  node1-relatship = cl_gui_simple_tree=>relat_last_child.
  node1-hidden = ''.
  node1-disabled = ''.
  node1-isfolder = ''.
  node1-n_image = '@DF@'.
  node1-exp_image = '@DF@'.
  CLEAR node1-expander.
  node1-text = '西城区'.
  APPEND node1 TO node_table.

  CLEAR node1.
  node1-node_key = 'MXQ'.
  node1-relatkey = 'SH'.
  node1-relatship = cl_gui_simple_tree=>relat_last_child.
  node1-hidden = ''.
  node1-disabled = ''.
  node1-isfolder = ''.
  node1-n_image = '@NL@'.
  node1-exp_image = '@NL@'.
  CLEAR node1-expander.
  node1-text = '闵行区'.
  APPEND node1 TO node_table.

*按照节点内容添加节点
  CALL METHOD wa_tree->add_nodes
    EXPORTING
      table_structure_name = 'MTREESNODE'
      node_table           = node_table.

  IF sy-subrc <> 0.
  ENDIF.

*定义双击事件
  event1-eventid = cl_gui_simple_tree=>eventid_node_double_click.
  event1-appl_event = 'X'.
  APPEND event1 TO events.
*添加事件内表
  CALL METHOD wa_tree->set_registered_events
    EXPORTING
      events = events.

  IF sy-subrc <> 0.
  ENDIF.

  CALL METHOD wa_tree->expand_node
    EXPORTING
      node_key            = 'CHINA'
      expand_subtree      = 'X'
    EXCEPTIONS
      failed              = 1
      illegal_level_count = 2
      cntl_system_error   = 3
      node_not_found      = 4
      cannot_expand_leaf  = 5.

  IF sy-subrc NE 0.
  ENDIF.


*定义双击事件
  event1-eventid = cl_gui_simple_tree=>eventid_node_double_click.
  event1-appl_event = 'X'.
  APPEND event1 TO events.

  event1-eventid = cl_gui_simple_tree=>eventid_node_context_menu_req.
  event1-appl_event = ''.
  APPEND event1 TO events.

  CALL METHOD wa_tree->set_ctx_menu_select_event_appl
    EXPORTING
      appl_event = 'X'.

*添加事件内表
  CALL METHOD wa_tree->set_registered_events
    EXPORTING
      events = events.


*将已定义的双击事件分配至树对象
  SET HANDLER g_application->handle_node_double_click FOR wa_tree.

  SET HANDLER g_application->handle_node_context_menu_req FOR wa_tree.

  SET HANDLER g_application->handle_node_context_menu_sel FOR wa_tree.

ENDFORM.                    "CREATE_TREE

4、Tree Control对象与ALV Grid对象关联

在双击Tree对象时,变更ALV Grid对象的显示,实现界面如图

ABAP Tree Control的使用_第10张图片

建立好的屏幕和对象界面如图:

ABAP Tree Control的使用_第11张图片
元素清单如图:


逻辑流程序:

PROCESS BEFORE OUTPUT.
 MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
 MODULE USER_COMMAND_0100.
主程序:

REPORT  Z_TREETEST_20160630002.
DATA: OK_CODE TYPE SY-UCOMM,
      SAVE_OK TYPE SY-UCOMM.

DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE',
      WA_CONTAINERALV TYPE SCRFNAME VALUE 'ALV',
      WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      WA_CUSTOM_CONTAINERALV TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE,
      WA_ALV TYPE REF TO CL_GUI_ALV_GRID.

DATA: NODE_TABLE LIKE TABLE OF MTREESNODE,
      NODE1 TYPE MTREESNODE.

DATA WA_SPFLI TYPE TABLE OF SPFLI WITH HEADER LINE.
DATA WA_SFLIGHT TYPE TABLE OF SFLIGHT.

DATA: NODEKEY(200) VALUE 'NODE',
      NODETEXT(200) VALUE 'NODE'.

CLASS LCL_APPLICATION DEFINITION DEFERRED.
DATA EVENT_RECEIVER TYPE REF TO LCL_APPLICATION.

CLASS LCL_APPLICATION DEFINITION.
  PUBLIC SECTION.

  METHODS HANDLE_NODE_DOUBLE_CLICK
      FOR EVENT NODE_DOUBLE_CLICK
      OF CL_GUI_SIMPLE_TREE
      IMPORTING NODE_KEY.
ENDCLASS.

CLASS LCL_APPLICATION IMPLEMENTATION.
  METHOD HANDLE_NODE_DOUBLE_CLICK.
    NODEKEY = NODE_KEY.
    READ TABLE NODE_TABLE WITH KEY NODE_KEY = NODE_KEY INTO NODE1.
    NODETEXT = NODE1-TEXT.

    CLEAR WA_SFLIGHT.
    SELECT * INTO TABLE WA_SFLIGHT FROM SFLIGHT
      WHERE CARRID = NODE1-NODE_KEY+2(2)
        AND CONNID = NODE1-NODE_KEY+4(4).

    CALL METHOD WA_ALV->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        I_STRUCTURE_NAME = 'SFLIGHT'
      CHANGING
        IT_OUTTAB = WA_SFLIGHT.
   ENDMETHOD.
ENDCLASS.

DATA G_APPLICATION TYPE REF TO LCL_APPLICATION.

START-OF-SELECTION.
  SELECT * INTO TABLE WA_SPFLI FROM SPFLI
    ORDER BY COUNTRYFR CARRID ASCENDING.

  CREATE OBJECT G_APPLICATION.
  SET SCREEN 100.

MODULE STATUS_0100 OUTPUT.
  IF WA_CUSTOM_CONTAINER IS INITIAL.
    PERFORM CREATE_TREE.
  ENDIF.
ENDMODULE.

MODULE USER_COMMAND_0100 INPUT.
  SAVE_OK = OK_CODE.
  CLEAR OK_CODE.
  CASE SAVE_OK.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.

FORM CREATE_TREE.
  DATA: EVENTS TYPE CNTL_SIMPLE_EVENTS,
        EVENT1 TYPE CNTL_SIMPLE_EVENT.

*建立定制控制对象
  CREATE OBJECT WA_CUSTOM_CONTAINER
  EXPORTING
    CONTAINER_NAME = WA_CONTAINER.

*创建ALV对象
  CREATE OBJECT WA_CUSTOM_CONTAINERALV
  EXPORTING
    CONTAINER_NAME = WA_CONTAINERALV.

  CREATE OBJECT WA_ALV
  EXPORTING
    I_PARENT = WA_CUSTOM_CONTAINERALV.

*建立树对象
  CREATE OBJECT WA_TREE
  EXPORTING
    PARENT = WA_CUSTOM_CONTAINER
    NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE.

  IF SY-SUBRC <> 0.
  ENDIF.

  DATA: COUNTRYFR LIKE SPFLI-COUNTRYFR,
        CARRID LIKE SPFLI-CARRID.

  DATA:STR1(255).

  LOOP AT WA_SPFLI.
    IF COUNTRYFR NE WA_SPFLI-COUNTRYFR.
      CLEAR NODE1.
      NODE1-NODE_KEY = WA_SPFLI-COUNTRYFR.
*第一层
      CLEAR NODE1-RELATKEY.
      CLEAR NODE1-RELATSHIP.
*是文件夹
      NODE1-HIDDEN = ''.
      NODE1-DISABLED = ''.
      NODE1-ISFOLDER = 'X'.
*不带ICON
      CLEAR NODE1-N_IMAGE.
      CLEAR NODE1-EXP_IMAGE.
      CLEAR NODE1-EXPANDER.

      NODE1-TEXT = WA_SPFLI-COUNTRYFR.
      APPEND NODE1 TO NODE_TABLE.
    ENDIF.

    IF ( COUNTRYFR NE WA_SPFLI-COUNTRYFR ) OR ( CARRID <> WA_SPFLI-CARRID ).
      CLEAR NODE1.
      CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID INTO NODE1-NODE_KEY.
*上层关系
      NODE1-RELATKEY = WA_SPFLI-COUNTRYFR.
      NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
      NODE1-HIDDEN = ''.
      NODE1-DISABLED = ''.
      NODE1-ISFOLDER = 'X'.
      CLEAR NODE1-N_IMAGE.
      CLEAR NODE1-EXP_IMAGE.
      CLEAR NODE1-EXPANDER.
*节点显示CARRID
      NODE1-TEXT = WA_SPFLI-CARRID.
      APPEND NODE1 TO NODE_TABLE.
    ENDIF.

    COUNTRYFR = WA_SPFLI-COUNTRYFR.
    CARRID = WA_SPFLI-CARRID.
    CLEAR NODE1.
    CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID
    WA_SPFLI-CONNID INTO NODE1-NODE_KEY.

    CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID INTO STR1.
    NODE1-RELATKEY = STR1.
    NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
    NODE1-HIDDEN = ''.
    NODE1-DISABLED = ''.
    NODE1-ISFOLDER = 'X'.
    NODE1-N_IMAGE = '@5B@'.
    NODE1-EXP_IMAGE = '@5B@'.
    CLEAR NODE1-EXPANDER.
    CONCATENATE WA_SPFLI-CARRID ':' WA_SPFLI-CITYFROM INTO STR1.
    NODE1-TEXT = STR1.
    APPEND NODE1 TO NODE_TABLE.
  ENDLOOP.

*按照节点内容添加节点
  CALL METHOD WA_TREE->ADD_NODES
  EXPORTING
    TABLE_STRUCTURE_NAME = 'MTREESNODE'
    NODE_TABLE = NODE_TABLE.
  IF SY-SUBRC <> 0.
  ENDIF.

*定义双击事件
  EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK.
  EVENT1-APPL_EVENT = 'X'.
  APPEND EVENT1 TO EVENTS.

  CALL METHOD WA_TREE->SET_REGISTERED_EVENTS
    EXPORTING
      EVENTS = EVENTS.

  IF SY-SUBRC <> 0.
  ENDIF.

*将已定义的双击事件分配至树对象
  SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR WA_TREE.
ENDFORM.

你可能感兴趣的:(tree,树,SAP,abap,control)