通过右键菜单完成Tree Control 节点的增删改功能
Tree 节点的增删改是很重要的功能,包括:增加本级节点,增加子节点,修改节点,删
除节点。完成后效果如下:
选择根节点,单击右键,弹出右键菜单,如选择“北京”节点,选择“增加本级节点”
菜单,如图9-5 所示。
在单击文本输入“广州”后,单击“更新”按钮,如图9-6 所示。
Tree 对象增加了“广州”节点,选择该节点,选右键菜单中“增加子节点”菜单,如图
9-7 所示。
输入“天河区”,单击“更新”按钮,如图9-8 所示。
增加节点后,输出结果如图9-9 所示。
除上述功能外,还有修改及删除节点等功能。
设计 Screen 界面如图9-10 所示。
主程序:
REPORT ytest35.
DATA: ok_code TYPE sy-ucomm,save_ok TYPE sy-ucomm.
DATA: wa_container TYPE scrfname VALUE 'TREE1',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_clickFOR EVENT node_double_clickOF cl_gui_simple_treeIMPORTING node_key.METHODS:handle_node_context_menu_reqFOR EVENT node_context_menu_requestOF cl_gui_simple_treeIMPORTING node_key menu,handle_node_context_menu_selFOR EVENT node_context_menu_selectOF cl_gui_simple_treeIMPORTING 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_CLICKMETHOD handle_node_context_menu_req.CALL METHOD menu->add_functionEXPORTINGtext = '增加本级节点'fcode = 'CODE1'.CALL METHOD menu->add_functionEXPORTINGtext = '增加子节点'fcode = 'CODE2'.CALL METHOD menu->add_functionEXPORTINGtext = '修改当前节点'fcode = 'CODE3'.CALL METHOD menu->add_functionEXPORTINGtext = '删除当前节点'fcode = 'CODE4'.ENDMETHOD. "handle_node_context_menu_reqMETHOD handle_node_context_menu_sel.DATA str1 TYPE tv_nodekey.CALL METHOD wa_tree->get_selected_nodeIMPORTINGnode_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_nodeEXPORTINGnode_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
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.IF wa_custom_container IS INITIAL.CREATE OBJECT g_application.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 'EXIT'.LEAVE PROGRAM.WHEN 'GETKEY'.CALL METHOD wa_tree->get_selected_nodeIMPORTINGnode_key = str1.IF sy-subrc EQ 0.getkey = str1.ENDIF.WHEN 'UPTREE'.IF ( menustr = 'CODE1' ) OR ( menustr = 'CODE2').READ TABLE node_tableWITH 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 = nodekeyoldINTO 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_nodesEXPORTINGtable_structure_name = 'MTREESNODE'node_table = anode_table.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.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_nodesEXPORTINGtable_structure_name = 'MTREESNODE'node_table = anode_table.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.WHEN 'CODE3'.node1-node_key = nodekeyold.CALL METHOD wa_tree->node_set_textEXPORTINGnode_key = node1-node_keytext = nodetext.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.ENDCASE.ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form CREATE_TREE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_tree .DATA: events TYPE cntl_simple_events,event1 TYPE cntl_simple_event.CREATE OBJECT wa_custom_containerEXPORTINGcontainer_name = wa_container.CREATE OBJECT wa_treeEXPORTINGparent = wa_custom_containernode_selection_mode = cl_gui_simple_tree=>node_sel_mode_single.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.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 = ''.
*图标,可运行SHOWICON查阅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_nodesEXPORTINGtable_structure_name = 'MTREESNODE'node_table = node_table
* EXCEPTIONS
* ERROR_IN_NODE_TABLE = 1
* FAILED = 2
* DP_ERROF = 3
* TABLE_STRUCTURE_NAME_NOT_FOUND = 4
* OTHERS = 5.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.CALL METHOD wa_tree->expand_nodeEXPORTINGnode_key = '国家'expand_subtree = 'X'EXCEPTIONSfailed = 1illegal_level_count = 2cntl_system_error = 3node_not_found = 4cannot_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_applEXPORTINGappl_event = 'X'.
*添加事件内表CALL METHOD wa_tree->set_registered_eventsEXPORTINGevents = events
* EXCEPTIONS
* CNTL_ERROR = 1
* CNTL_SYSTEM_ERROR = 2
* ILLEGAL_EVENT_COMBINATION = 3
* OTHERS = 4.IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.
*将已定义的双击事件分配至树对象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