Abap/4 控制ALV树(2)

*&---------------------------------------------------------------------*
*& Report  Z_CPT10_1                                                   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&         ALV树+树节点双击事件+添加树控件的弹出菜单+节点的CRUD操作

*&                                                                     *
*&---------------------------------------------------------------------*

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

"定制ALV控件
DATA:WA_CONTAINER TYPE SCRFNAME VALUE 'CONTAINER1',
     WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
     WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
                         "CL_GUI_ALV_TREE_SIMPLE
"定义节点
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 'TEXT',
     NODEKEYOLD(200) VALUE 'NODE',
     GETKEY(200).

DATA MENUSTR(10) VALUE ''.

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.
        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.

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.
    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.

    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 = STR1 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.
ENDCLASS.

DATA:G_APPLICATION TYPE REF TO LCL_APPLICATION.

START-OF-SELECTION.
    CREATE OBJECT G_APPLICATION.
    CALL SCREEN 100.


*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
*  SET PF-STATUS 'xxxxxxxx'.
*  SET TITLEBAR 'xxx'.

IF WA_CUSTOM_CONTAINER IS INITIAL.
    PERFORM CREATE_TREE.
ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
    DATA STR1 TYPE TV_NODEKEY.
    SAVE_OK = OK_CODE.
    CLEAR OK_CODE.

    CASE SAVE_OK.
        WHEN 'GETKEY'.
           CALL METHOD WA_TREE->GET_SELECTED_NODE
               IMPORTING
                   NODE_KEY = STR1.
           IF SY-SUBRC = 0."这个应该是判断是否出错
               GETKEY = STR1.
           ENDIF.
        WHEN 'UPTREE'.
            IF ( MENUSTR = 'CODE1' ) OR ( MENUSTR = 'CODE2' ).
                READ TABLE NODE_TABLE
                    WITH KEY NODE_KEY = NODEKEY INTO NODE1.
                IF SY-SUBRC = 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.
            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.
            WHEN 'CODE3'.
                NODE1-NODE_KEY = NODEKEYOLD.
                "修改节点的名称
                CALL METHOD WA_TREE->NODE_SET_TEXT
                    EXPORTING
                        NODE_KEY = NODE1-NODE_KEY
                        TEXT = NODETEXT.
        ENDCASE.

    ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  CANCEL  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE CANCEL INPUT.
    LEAVE PROGRAM.
ENDMODULE.                 " CANCEL  INPUT

 


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.

CLEAR NODE1.
NODE1-NODE_KEY = '国家'.

"第一层
CLEAR NODE1-RELATKEY.
CLEAR NODE1-RELATSHIP.

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 = '城市北京'.
"上层关系
NODE1-RELATKEY = '国家'.
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 = '城市上海'.
"上层关系
NODE1-RELATKEY = '国家'.
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 = 'KEY 崇文区'.
"上层关系
NODE1-RELATKEY = '城市北京'.
NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
NODE1-HIDDEN = ''.
NODE1-DISABLED = ''.
NODE1-ISFOLDER = ''.
NODE1-N_IMAGE = '@9Y@'.
NODE1-EXP_IMAGE = '@9Y@'.
CLEAR NODE1-EXPANDER.
NODE1-TEXT = '崇文区'.
APPEND NODE1 TO NODE_TABLE.

CLEAR NODE1.
NODE1-NODE_KEY = 'KEY 西城区'.
"上层关系
NODE1-RELATKEY = '城市北京'.
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 = 'KEY 闵行区'.
NODE1-RELATKEY = '城市上海'.
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.

*展开树
CALL METHOD WA_TREE->EXPAND_NODE
    EXPORTING
        NODE_KEY = '国家'
        EXPAND_SUBTREE = 'X'.

*定义双击事件
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.

你可能感兴趣的:(image,tree,table,application,menu,structure)