二维码

[Tree] 建立有数据表接口的Tree Control 对象

Twilight发表于 2014-01-22 16:43zhongguomao 最后回复于 2017-10-09 10:19 [复制链接] 856 1

先从数据表读取结构,并按照数据关系建立 Tree Control,如下图所示。
tree_control_2.jpg
以航班表为例,以“起飞国家—>航班承运人—>航班连接”三层组成。

注:如果阅读该案例有困难,可先阅读使用oo实现简单的Tree control实例

主程序:
  1. REPORT  ZDEMON_010.
  2. *功能码返回值
  3. DATA: OK_CODE TYPE SY-UCOMM,
  4. SAVE_OK TYPE SY-UCOMM.
  5. *定制对象
  6. DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE1',
  7. *客户对象
  8. WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
  9. WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
  10. *NODE 节点表
  11. DATA: NODE_TABLE LIKE TABLE OF MTREESNODE,
  12. *节点
  13. NODE1 TYPE MTREESNODE.
  14. DATA WA_SPFLI TYPE TABLE OF SPFLI WITH HEADER LINE.
  15. START-OF-SELECTION.
  16. SELECT * INTO TABLE WA_SPFLI FROM SPFLI
  17. ORDER BY COUNTRYFR CARRID CONNID ASCENDING.
  18. SET SCREEN 100.
  19. *&---------------------------------------------------------------------*
  20. *& Module STATUS_0100 OUTPUT
  21. *&---------------------------------------------------------------------*
  22. * text
  23. *----------------------------------------------------------------------*
  24. MODULE STATUS_0100 OUTPUT.
  25. * SET PF-STATUS 'STATUS1'.
  26. * CLEAR WA_CUSTOM_CONTAINER.
  27. IF WA_CUSTOM_CONTAINER IS INITIAL.
  28. PERFORM CREATE_TREE.
  29. ENDIF.
  30. ENDMODULE. " STATUS_0100 OUTPUT
  31. *&---------------------------------------------------------------------*
  32. *& Module USER_COMMAND_0100 INPUT
  33. *&---------------------------------------------------------------------*
  34. * 用户交互
  35. *----------------------------------------------------------------------*
  36. MODULE USER_COMMAND_0100 INPUT.
  37. SAVE_OK = OK_CODE.
  38. CLEAR OK_CODE.
  39. CASE SAVE_OK.
  40. WHEN 'EXIT'.
  41. * LEAVE PROGRAM.
  42. LEAVE TO SCREEN 0.
  43. ENDCASE.
  44. ENDMODULE. " USER_COMMAND_0100 INPUT
  45. *&---------------------------------------------------------------------*
  46. *& Form CREATE_TREE
  47. *&---------------------------------------------------------------------*
  48. * 创建树及子节点
  49. *----------------------------------------------------------------------*
  50. * --> p1 text
  51. * <-- p2 text
  52. *----------------------------------------------------------------------*
  53. FORM CREATE_TREE .
  54. *建立定制控制对象
  55. CREATE OBJECT WA_CUSTOM_CONTAINER
  56. EXPORTING
  57. CONTAINER_NAME = WA_CONTAINER.
  58. *建立树对象
  59. CREATE OBJECT WA_TREE
  60. EXPORTING
  61. * LIFETIME =
  62. PARENT = WA_CUSTOM_CONTAINER
  63. * SHELLSYLE =
  64. NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE
  65. * HIDE_SELECTION =
  66. * NAME =
  67. * EXCEPTIONS
  68. * LIFETIME_ERROR = 1
  69. * CNTL_SYSTEM_ERROR = 2
  70. * CREATE_ERROR = 3
  71. * FAILED = 4
  72. * ILLEGAL_NOCE_SELECTION_MODE = 5
  73. * OTHERS = 6
  74. .
  75. IF SY-SUBRC <> 0.
  76. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  77. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  78. ENDIF.
  79. DATA: COUNTRYFR LIKE SPFLI-COUNTRYFR,
  80. CARRID LIKE SPFLI-CARRID.
  81. DATA: STR1(255).
  82. LOOP AT WA_SPFLI.
  83. IF COUNTRYFR NE WA_SPFLI-COUNTRYFR.
  84. CLEAR NODE1.
  85. NODE1-NODE_KEY = WA_SPFLI-COUNTRYFR.
  86. *第一层
  87. CLEAR NODE1-RELATKEY.
  88. CLEAR NODE1-RELATSHIP.
  89. *是文件夹
  90. NODE1-HIDDEN = ''.
  91. NODE1-DISABLED = ''.
  92. NODE1-ISFOLDER = 'X'.
  93. *不带ICON
  94. CLEAR NODE1-N_IMAGE.
  95. CLEAR NODE1-EXP_IMAGE.
  96. CLEAR NODE1-EXPANDER.
  97. NODE1-TEXT = WA_SPFLI-COUNTRYFR.
  98. APPEND NODE1 TO NODE_TABLE.
  99. ENDIF.
  100. IF ( COUNTRYFR NE WA_SPFLI-COUNTRYFR ) OR
  101. ( CARRID <> WA_SPFLI-CARRID ).
  102. CLEAR NODE1.
  103. CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID
  104. INTO NODE1-NODE_KEY.
  105. *上层关系
  106. NODE1-RELATKEY = WA_SPFLI-COUNTRYFR.
  107. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  108. NODE1-HIDDEN = ''.
  109. NODE1-DISABLED = ''.
  110. NODE1-ISFOLDER = 'X'.
  111. CLEAR NODE1-N_IMAGE.
  112. CLEAR NODE1-EXP_IMAGE.
  113. CLEAR NODE1-EXPANDER.
  114. *节点显示CARRID
  115. NODE1-TEXT = WA_SPFLI-CARRID.
  116. APPEND NODE1 TO NODE_TABLE.
  117. ENDIF.
  118. COUNTRYFR = WA_SPFLI-COUNTRYFR.
  119. CARRID = WA_SPFLI-CARRID.
  120. CLEAR NODE1.
  121. CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID
  122. WA_SPFLI-CONNID INTO NODE1-NODE_KEY.
  123. CONCATENATE WA_SPFLI-COUNTRYFR WA_SPFLI-CARRID
  124. INTO STR1.
  125. NODE1-RELATKEY = STR1.
  126. NODE1-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
  127. NODE1-HIDDEN = ''.
  128. NODE1-DISABLED = ''.
  129. NODE1-ISFOLDER = 'X'.
  130. NODE1-N_IMAGE = '@5B@'.
  131. NODE1-EXP_IMAGE = '@5B@'.
  132. CLEAR NODE1-EXPANDER.
  133. CONCATENATE WA_SPFLI-CARRID ':' WA_SPFLI-CITYFROM
  134. INTO STR1.
  135. NODE1-TEXT = STR1.
  136. APPEND NODE1 TO NODE_TABLE.
  137. ENDLOOP.
  138. *按照节点内容添加节点
  139. CALL METHOD WA_TREE->ADD_NODES
  140. EXPORTING
  141. TABLE_STRUCTURE_NAME = 'MTREESNODE'
  142. NODE_TABLE = NODE_TABLE
  143. * EXCEPTIONS
  144. * ERROR_IN_NODE_TABLE = 1
  145. * FAILED = 2
  146. * DP_ERROF = 3
  147. * TABLE_STRUCTURE_NAME_NOT_FOUND = 4
  148. * OTHERS = 5
  149. .
  150. IF SY-SUBRC <> 0.
  151. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  152. * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  153. ENDIF.
  154. ENDFORM. " CREATE_TREE
复制代码



回复

使用道具 举报

zhongguomao
这个例子最早接触的是王宏的一本书里面的。哈哈哈。
回复 支持 反对

使用道具 举报

快速回帖

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

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