二维码

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

Twilight发表于 2014-01-22 17:03Twilight 最后回复于 2014-01-22 17:03 [复制链接] 3928 0

Tree 节点的增删改是很重要的功能,包括:增加本级节点,增加子节点,修改节点,删除节点。完成后效果如下:
选择根节点,单击右键,弹出右键菜单,如选择“北京”节点,选择“增加本级节点”菜单,如图所示。

右键完成Tree Control 节点的增删改功能1.jpg
在单击文本输入“广州”后,单击“更新”按钮,如图所示。

右键完成Tree Control 节点的增删改功能2.jpg

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

右键完成Tree Control 节点的增删改功能3.jpg

输入“天河区”,单击“更新”按钮,如所示。

右键完成Tree Control 节点的增删改功能4.jpg

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

右键完成Tree Control 节点的增删改功能5.jpg

除上述功能外,还有修改及删除节点等功能。
设计 Screen 界面如所示。
右键完成Tree Control 节点的增删改功能6.jpg

主程序:
  1. REPORT  ZDEMON_011.
  2. DATA: OK_CODE TYPE SY-UCOMM,
  3. SAVE_OK TYPE SY-UCOMM.
  4. DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE1',
  5. WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
  6. WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
  7. DATA: NODE_TABLE LIKE TABLE OF MTREESNODE,
  8. ANODE_TABLE LIKE TABLE OF MTREESNODE,
  9. NODE1 TYPE MTREESNODE,
  10. NODE2 TYPE MTREESNODE.
  11. DATA WA_SPFLI TYPE TABLE OF SPFLI.
  12. DATA: NODEKEY(200) VALUE 'NODE',
  13. NODETEXT(200) VALUE 'NODE',
  14. NODEKEYOLD(200) VALUE 'NODE',
  15. GETKEY(200).
  16. DATA MENUSTR(10) VALUE ''.
  17. CLASS LCL_APPLICATION DEFINITION DEFERRED.
  18. *---------------------------------------------------------------------*
  19. * CLASS lcl_application DEFINITION
  20. *---------------------------------------------------------------------*
  21. *
  22. *---------------------------------------------------------------------*
  23. CLASS LCL_APPLICATION DEFINITION.
  24. PUBLIC SECTION.
  25. METHODS: HANDLE_NODE_DOUBLE_CLICK
  26. FOR EVENT NODE_DOUBLE_CLICK
  27. OF CL_GUI_SIMPLE_TREE
  28. IMPORTING NODE_KEY.
  29. METHODS:
  30. HANDLE_NODE_CONTEXT_MENU_REQ
  31. FOR EVENT NODE_CONTEXT_MENU_REQUEST
  32. OF CL_GUI_SIMPLE_TREE
  33. IMPORTING NODE_KEY MENU,
  34. HANDLE_NODE_CONTEXT_MENU_SEL
  35. FOR EVENT NODE_CONTEXT_MENU_SELECT
  36. OF CL_GUI_SIMPLE_TREE
  37. IMPORTING NODE_KEY FCODE.
  38. ENDCLASS. "LCL_APPLICATION DEFINITION
  39. *---------------------------------------------------------------------*
  40. * CLASS lcl_application IMPLEMENTATION
  41. *---------------------------------------------------------------------*
  42. *
  43. *---------------------------------------------------------------------*
  44. CLASS LCL_APPLICATION IMPLEMENTATION.
  45. METHOD HANDLE_NODE_DOUBLE_CLICK.
  46. NODEKEY = NODE_KEY.
  47. READ TABLE NODE_TABLE WITH KEY NODE_KEY = NODE_KEY INTO NODE1.
  48. NODETEXT = NODE1-TEXT.
  49. ENDMETHOD. "HANDLE_NODE_DOUBLE_CLICK
  50. METHOD HANDLE_NODE_CONTEXT_MENU_REQ.
  51. CALL METHOD MENU->ADD_FUNCTION
  52. EXPORTING
  53. TEXT = '增加本级节点'
  54. FCODE = 'CODE1'.
  55. CALL METHOD MENU->ADD_FUNCTION
  56. EXPORTING
  57. TEXT = '增加子节点'
  58. FCODE = 'CODE2'.
  59. CALL METHOD MENU->ADD_FUNCTION
  60. EXPORTING
  61. TEXT = '修改当前节点'
  62. FCODE = 'CODE3'.
  63. CALL METHOD MENU->ADD_FUNCTION
  64. EXPORTING
  65. TEXT = '删除当前节点'
  66. FCODE = 'CODE4'.
  67. ENDMETHOD. "handle_node_context_menu_req
  68. METHOD HANDLE_NODE_CONTEXT_MENU_SEL.
  69. DATA STR1 TYPE TV_NODEKEY.
  70. CALL METHOD WA_TREE->GET_SELECTED_NODE
  71. IMPORTING
  72. NODE_KEY = STR1.
  73. IF SY-SUBRC = 0.
  74. MENUSTR = FCODE.
  75. NODEKEY = STR1.
  76. NODEKEYOLD = STR1.
  77. READ TABLE NODE_TABLE WITH KEY NODE_KEY = STR1 INTO NODE1.
  78. NODETEXT = NODE1-TEXT.
  79. IF FCODE = 'CODE4'.
  80. CALL METHOD WA_TREE->DELETE_NODE
  81. EXPORTING
  82. NODE_KEY = NODE1-NODE_KEY.
  83. DELETE NODE_TABLE WHERE NODE_KEY = NODEKEY.
  84. ENDIF.
  85. ENDIF.
  86. ENDMETHOD. "handle_node_context_menu_sel
  87. ENDCLASS. "lcl_application IMPLEMENTATION
  88. *自定义定义对象实例
  89. DATA: G_APPLICATION TYPE REF TO LCL_APPLICATION.
  90. START-OF-SELECTION.
  91. CALL SCREEN 100.
  92. *&---------------------------------------------------------------------*
  93. *& Module STATUS_0100 OUTPUT
  94. *&---------------------------------------------------------------------*
  95. * text
  96. *----------------------------------------------------------------------*
  97. MODULE STATUS_0100 OUTPUT.
  98. * SET PF-STATUS 'xxxxxxxx'.
  99. * SET TITLEBAR 'xxx'.
  100. IF WA_CUSTOM_CONTAINER IS INITIAL.
  101. CREATE OBJECT G_APPLICATION.
  102. PERFORM CREATE_TREE.
  103. ENDIF.
  104. ENDMODULE. " STATUS_0100 OUTPUT
  105. *&---------------------------------------------------------------------*
  106. *& Module USER_COMMAND_0100 INPUT
  107. *&---------------------------------------------------------------------*
  108. * text
  109. *----------------------------------------------------------------------*
  110. MODULE USER_COMMAND_0100 INPUT.
  111. DATA STR1 TYPE TV_NODEKEY.
  112. SAVE_OK = OK_CODE.
  113. CLEAR OK_CODE.
  114. CASE SAVE_OK.
  115. WHEN 'EXIT'.
  116. LEAVE PROGRAM.
  117. WHEN 'GETKEY'.
  118. CALL METHOD WA_TREE->GET_SELECTED_NODE
  119. IMPORTING
  120. NODE_KEY = STR1.
  121. IF SY-SUBRC EQ 0.
  122. GETKEY = STR1.
  123. ENDIF.
  124. WHEN 'UPTREE'.
  125. IF ( MENUSTR = 'CODE1' ) OR ( MENUSTR = 'CODE2').
  126. READ TABLE NODE_TABLE
  127. WITH KEY NODE_KEY = NODEKEY INTO NODE1.
  128. IF SY-SUBRC EQ 0.
  129. MESSAGE E208(00) WITH '键值重复!'.
  130. ENDIF.
  131. ENDIF.
  132. CASE MENUSTR.
  133. WHEN 'CODE1'.
  134. READ TABLE NODE_TABLE WITH KEY NODE_KEY = NODEKEYOLD
  135. INTO NODE2.
  136. CLEAR NODE1.
  137. NODE1-NODE_KEY = NODEKEY.
  138. NODE1-RELATKEY = NODE2-RELATKEY.
  139. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  140. NODE1-HIDDEN = ''.
  141. NODE1-DISABLED = ''.
  142. NODE1-ISFOLDER = 'X'.
  143. NODE1-N_IMAGE = '@G3@'.
  144. NODE1-EXP_IMAGE = '@G3@'.
  145. CLEAR NODE1-EXPANDER.
  146. NODE1-TEXT = NODETEXT.
  147. APPEND NODE1 TO NODE_TABLE.
  148. CLEAR ANODE_TABLE.
  149. APPEND NODE1 TO ANODE_TABLE.
  150. CALL METHOD WA_TREE->ADD_NODES
  151. EXPORTING
  152. TABLE_STRUCTURE_NAME = 'MTREESNODE'
  153. NODE_TABLE = ANODE_TABLE.
  154. IF SY-SUBRC <> 0.
  155. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  156. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  157. ENDIF.
  158. WHEN 'CODE2'.
  159. CLEAR NODE1.
  160. NODE1-NODE_KEY = NODEKEY.
  161. NODE1-RELATKEY = NODEKEYOLD..
  162. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  163. NODE1-HIDDEN = ''.
  164. NODE1-DISABLED = ''.
  165. NODE1-ISFOLDER = 'X'.
  166. NODE1-N_IMAGE = '@G3@'.
  167. NODE1-EXP_IMAGE = '@G3@'.
  168. CLEAR NODE1-EXPANDER.
  169. NODE1-TEXT = NODETEXT.
  170. APPEND NODE1 TO NODE_TABLE.
  171. CLEAR ANODE_TABLE.
  172. APPEND NODE1 TO ANODE_TABLE.
  173. CALL METHOD WA_TREE->ADD_NODES
  174. EXPORTING
  175. TABLE_STRUCTURE_NAME = 'MTREESNODE'
  176. NODE_TABLE = ANODE_TABLE.
  177. IF SY-SUBRC <> 0.
  178. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  179. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  180. ENDIF.
  181. WHEN 'CODE3'.
  182. NODE1-NODE_KEY = NODEKEYOLD.
  183. CALL METHOD WA_TREE->NODE_SET_TEXT
  184. EXPORTING
  185. NODE_KEY = NODE1-NODE_KEY
  186. TEXT = NODETEXT.
  187. IF SY-SUBRC <> 0.
  188. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  189. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  190. ENDIF.
  191. ENDCASE.
  192. ENDCASE.
  193. ENDMODULE. " USER_COMMAND_0100 INPUT
  194. *&---------------------------------------------------------------------*
  195. *& Form CREATE_TREE
  196. *&---------------------------------------------------------------------*
  197. * text
  198. *----------------------------------------------------------------------*
  199. * --> p1 text
  200. * <-- p2 text
  201. *----------------------------------------------------------------------*
  202. FORM CREATE_TREE .
  203. DATA: EVENTS TYPE CNTL_SIMPLE_EVENTS,
  204. EVENT1 TYPE CNTL_SIMPLE_EVENT.
  205. CREATE OBJECT WA_CUSTOM_CONTAINER
  206. EXPORTING
  207. CONTAINER_NAME = WA_CONTAINER.
  208. CREATE OBJECT WA_TREE
  209. EXPORTING
  210. PARENT = WA_CUSTOM_CONTAINER
  211. NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE.
  212. IF SY-SUBRC <> 0.
  213. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  214. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  215. ENDIF.
  216. CLEAR NODE1.
  217. NODE1-NODE_KEY = '国家'.
  218. CLEAR NODE1-RELATKEY.
  219. CLEAR NODE1-RELATSHIP.
  220. NODE1-HIDDEN = ''.
  221. NODE1-DISABLED = ''.
  222. NODE1-ISFOLDER = 'X'.
  223. CLEAR NODE1-N_IMAGE.
  224. CLEAR NODE1-EXP_IMAGE.
  225. CLEAR NODE1-EXPANDER.
  226. NODE1-TEXT = '中国'.
  227. APPEND NODE1 TO NODE_TABLE.
  228. CLEAR NODE1.
  229. NODE1-NODE_KEY = '城市北京'.
  230. NODE1-RELATKEY = '国家'.
  231. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  232. NODE1-HIDDEN = ''.
  233. NODE1-DISABLED = ''.
  234. NODE1-ISFOLDER = 'X'.
  235. CLEAR NODE1-N_IMAGE.
  236. CLEAR NODE1-EXP_IMAGE.
  237. CLEAR NODE1-EXPANDER.
  238. NODE1-TEXT = '北京'.
  239. APPEND NODE1 TO NODE_TABLE.
  240. CLEAR NODE1.
  241. NODE1-NODE_KEY = '城市上海'.
  242. NODE1-RELATKEY = '国家'.
  243. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  244. NODE1-HIDDEN = ''.
  245. NODE1-DISABLED = ''.
  246. NODE1-ISFOLDER = 'X'.
  247. CLEAR NODE1-N_IMAGE.
  248. CLEAR NODE1-EXP_IMAGE.
  249. CLEAR NODE1-EXPANDER.
  250. NODE1-TEXT = '上海'.
  251. APPEND NODE1 TO NODE_TABLE.
  252. CLEAR NODE1.
  253. NODE1-NODE_KEY = 'KEY 崇文区'.
  254. NODE1-RELATKEY = '城市北京'.
  255. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  256. NODE1-HIDDEN = ''.
  257. NODE1-DISABLED = ''.
  258. NODE1-ISFOLDER = ''.
  259. *图标,可运行SHOWICON查阅
  260. NODE1-N_IMAGE = '@9Y@'.
  261. NODE1-EXP_IMAGE = '@9Y@'.
  262. CLEAR NODE1-EXPANDER.
  263. NODE1-TEXT = '崇文区'.
  264. APPEND NODE1 TO NODE_TABLE.
  265. CLEAR NODE1.
  266. NODE1-NODE_KEY = 'KEY 西城区'.
  267. NODE1-RELATKEY = '城市北京'.
  268. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  269. NODE1-HIDDEN = ''.
  270. NODE1-DISABLED = ''.
  271. NODE1-ISFOLDER = ''.
  272. NODE1-N_IMAGE = '@DF@'.
  273. NODE1-EXP_IMAGE = '@DF@'.
  274. CLEAR NODE1-EXPANDER.
  275. NODE1-TEXT = '西城区'.
  276. APPEND NODE1 TO NODE_TABLE.
  277. CLEAR NODE1.
  278. NODE1-NODE_KEY = 'KEY 闵行区'.
  279. NODE1-RELATKEY = '城市上海'.
  280. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  281. NODE1-HIDDEN = ''.
  282. NODE1-DISABLED = ''.
  283. NODE1-ISFOLDER = ''.
  284. NODE1-N_IMAGE = '@NL@'.
  285. NODE1-EXP_IMAGE = '@NL@'.
  286. CLEAR NODE1-EXPANDER.
  287. NODE1-TEXT = '闵行区'.
  288. APPEND NODE1 TO NODE_TABLE.
  289. *按照节得内容掏加节得
  290. CALL METHOD WA_TREE->ADD_NODES
  291. EXPORTING
  292. TABLE_STRUCTURE_NAME = 'MTREESNODE'
  293. NODE_TABLE = NODE_TABLE
  294. * EXCEPTIONS
  295. * ERROR_IN_NODE_TABLE = 1
  296. * FAILED = 2
  297. * DP_ERROF = 3
  298. * TABLE_STRUCTURE_NAME_NOT_FOUND = 4
  299. * OTHERS = 5
  300. .
  301. IF SY-SUBRC <> 0.
  302. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  303. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  304. ENDIF.
  305. CALL METHOD WA_TREE->EXPAND_NODE
  306. EXPORTING
  307. NODE_KEY = '国家'
  308. EXPAND_SUBTREE = 'X'
  309. EXCEPTIONS
  310. FAILED = 1
  311. ILLEGAL_LEVEL_COUNT = 2
  312. CNTL_SYSTEM_ERROR = 3
  313. NODE_NOT_FOUND = 4
  314. CANNOT_EXPAND_LEAF = 5.
  315. IF SY-SUBRC NE 0.
  316. ENDIF.
  317. *定义双击事件
  318. EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK.
  319. EVENT1-APPL_EVENT = 'X'.
  320. APPEND EVENT1 TO EVENTS.
  321. EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_CONTEXT_MENU_REQ.
  322. EVENT1-APPL_EVENT = ''.
  323. APPEND EVENT1 TO EVENTS.
  324. CALL METHOD WA_TREE->SET_CTX_MENU_SELECT_EVENT_APPL
  325. EXPORTING
  326. APPL_EVENT = 'X'.
  327. *添加事件内表
  328. CALL METHOD WA_TREE->SET_REGISTERED_EVENTS
  329. EXPORTING
  330. EVENTS = EVENTS
  331. * EXCEPTIONS
  332. * CNTL_ERROR = 1
  333. * CNTL_SYSTEM_ERROR = 2
  334. * ILLEGAL_EVENT_COMBINATION = 3
  335. * OTHERS = 4
  336. .
  337. IF SY-SUBRC <> 0.
  338. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  339. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  340. ENDIF.
  341. *将已定义的双击事件分配至树对象
  342. SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR WA_TREE.
  343. SET HANDLER G_APPLICATION->HANDLE_NODE_CONTEXT_MENU_REQ FOR WA_TREE.
  344. SET HANDLER G_APPLICATION->HANDLE_NODE_CONTEXT_MENU_SEL FOR WA_TREE.
  345. ENDFORM. " CREATE_TREE
复制代码




回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表