Tree Control 是最常用的Windows控件之一,在其他语言中成为"Tree View"等,ABAP的 Tree Contiol 能实现类似的功能。
本文主要介绍一下内容:
1. 实例一个简单的 Tree Control,双击取得节点的Key 和 TEXT
2. 建立有数据表接口的Tree Control 对象
3. 通过右键菜单实现 Tree Control节点的 增 删 改 功能
4. Tree Control对象与ALV Grid对象关联
××××××××××××××××××××华丽的分割线×××××××××××××××××××××××
BEGIN
(一)实例一个简单的 Tree Control,双击取得节点的Key 和 TEXT.
1. 创建一个ABAP程序
2. 在程序中添加一个屏幕100,使用屏幕制作器,建立Screen,添加屏幕对象
创建好的屏幕和对象界面如图:
屏幕元素清单
逻辑流程序
PROCESS BEFORE OUTPUT. MODULE STATUS_0100. * PROCESS AFTER INPUT. MODULE USER_COMMAND_0100.
主程序
1 *&---------------------------------------------------------------------* 2 *& Report ZHAIM_TREE_CONTROL 3 *& 4 *&---------------------------------------------------------------------* 5 *& 6 *& 7 *&---------------------------------------------------------------------* 8 9 REPORT ZHAIM_TREE_CONTROL. 10 * 功能码 11 DATA: OK_CODE TYPE SY-UCOMM, 12 SAVE_OK TYPE SY-UCOMM. 13 * key text 14 DATA: NODEKEY(20) TYPE C, 15 NODETXT(20) TYPE C. 16 17 * 定制控制对象 18 DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE', 19 * 客户对象 20 WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER, 21 * 实例化对象 22 WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE. 23 * 节点信息表 24 DATA: IT_NODETABLE LIKE TABLE OF MTREESNODE, 25 IW_NODETABLE TYPE MTREESNODE. 26 27 DATA WA_SPFLI TYPE TABLE OF SPFLI. 28 29 *创建一个类,实现一个双击方法 30 CLASS LCL_APPLICATION DEFINITION DEFERRED. 31 32 *&---------------------------------------------------------------------* 33 *& Class (DEFINITION) LCL_APPLICATION 34 *&---------------------------------------------------------------------* 35 * Text 36 *----------------------------------------------------------------------* 37 CLASS LCL_APPLICATION DEFINITION. 38 PUBLIC SECTION. 39 METHODS HANDLE_NODE_DOUBLE_CLICK 40 FOR EVENT NODE_DOUBLE_CLICK 41 OF CL_GUI_SIMPLE_TREE 42 IMPORTING NODE_KEY. 43 ENDCLASS. "LCL_APPLICATION 44 *&---------------------------------------------------------------------* 45 *& Class (Implementation) LCL_APPLICATION 46 *&---------------------------------------------------------------------* 47 * Text 48 *----------------------------------------------------------------------* 49 CLASS LCL_APPLICATION IMPLEMENTATION. 50 51 METHOD HANDLE_NODE_DOUBLE_CLICK. 52 53 NODEKEY = NODE_KEY. 54 55 READ TABLE IT_NODETABLE WITH KEY NODE_KEY = NODE_KEY INTO IW_NODETABLE. 56 57 NODETXT = IW_NODETABLE-TEXT. 58 ENDMETHOD. 59 60 ENDCLASS. "LCL_APPLICATION 61 62 * 双击方法的类的实例化对象 准备在PBO中分配给TREE对象 63 DATA: G_APPLICATION TYPE REF TO LCL_APPLICATION. 64 65 66 START-OF-SELECTION. 67 68 CREATE OBJECT G_APPLICATION. 69 70 CALL SCREEN 100. 71 72 73 *&---------------------------------------------------------------------* 74 *& Module USER_COMMAND_0100 INPUT 75 *&---------------------------------------------------------------------* 76 * text 77 *----------------------------------------------------------------------* 78 MODULE USER_COMMAND_0100 INPUT. 79 80 SAVE_OK = OK_CODE. 81 82 CLEAR OK_CODE. 83 84 CASE SAVE_OK. 85 WHEN 'BACK'. 86 LEAVE PROGRAM. 87 WHEN OTHERS. 88 ENDCASE. 89 90 ENDMODULE. " USER_COMMAND_0100 INPUT 91 *&---------------------------------------------------------------------* 92 *& Module STATUS_0100 OUTPUT 93 *&---------------------------------------------------------------------* 94 * text 95 *----------------------------------------------------------------------* 96 MODULE STATUS_0100 OUTPUT. 97 SET PF-STATUS 'TREECTRL_GUIS'. 98 SET TITLEBAR 'TREE CONTROL'. 99 IF WA_CUSTOM_CONTAINER IS INITIAL. 100 PERFORM FRM_CREATE_TREE. 101 ENDIF. 102 ENDMODULE. " STATUS_0100 OUTPUT 103 104 *&---------------------------------------------------------------------* 105 *& Form FRM_CREATE_TREE 106 *&---------------------------------------------------------------------* 107 * text 108 *----------------------------------------------------------------------* 109 * --> p1 text 110 * <-- p2 text 111 *----------------------------------------------------------------------* 112 FORM FRM_CREATE_TREE . 113 DATA: EVENTS TYPE CNTL_SIMPLE_EVENTS, 114 EVENT1 TYPE CNTL_SIMPLE_EVENT. 115 * 定制控制对象,指定tree control的容器 116 CREATE OBJECT WA_CUSTOM_CONTAINER 117 EXPORTING 118 * PARENT = 119 CONTAINER_NAME = WA_CONTAINER " 定制对象名:TREE 120 * STYLE = 121 * LIFETIME = lifetime_default 122 * REPID = 123 * DYNNR = 124 * NO_AUTODEF_PROGID_DYNNR = 125 EXCEPTIONS 126 CNTL_ERROR = 1 127 CNTL_SYSTEM_ERROR = 2 128 CREATE_ERROR = 3 129 LIFETIME_ERROR = 4 130 LIFETIME_DYNPRO_DYNPRO_LINK = 5 131 others = 6 132 . 133 134 IF SY-SUBRC <> 0. 135 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 136 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 137 ENDIF. 138 139 * 定义树对象 140 CREATE OBJECT WA_TREE 141 EXPORTING 142 * LIFETIME = 143 PARENT = WA_CUSTOM_CONTAINER 144 * SHELLSTYLE = 145 NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE 146 * HIDE_SELECTION = 147 * NAME = 148 EXCEPTIONS 149 LIFETIME_ERROR = 1 150 CNTL_SYSTEM_ERROR = 2 151 CREATE_ERROR = 3 152 FAILED = 4 153 ILLEGAL_NODE_SELECTION_MODE = 5 154 others = 6 155 . 156 IF SY-SUBRC <> 0. 157 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 158 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 159 ENDIF. 160 161 * 通过宏定义树 节点 162 DEFINE CREATE_TREE. 163 164 CLEAR IW_NODETABLE. 165 166 IW_NODETABLE-NODE_KEY = &1. "节点KEY值 167 IW_NODETABLE-RELATKEY = &2. "相关联的节点KEY值 168 IW_NODETABLE-RELATSHIP = &3. "关联关系 169 IW_NODETABLE-HIDDEN = &4. 170 IW_NODETABLE-DISABLED = &5. 171 IW_NODETABLE-ISFOLDER = &6. "文件夹形式 172 * 图标,可以运行程序 SHOWICON 查阅 173 IW_NODETABLE-N_IMAGE = &7. "图标 174 IW_NODETABLE-EXP_IMAGE = &7. "图标 175 * IW_NODETABLE-EXPANDER. 176 IW_NODETABLE-TEXT = &8. "节点文本 177 178 APPEND IW_NODETABLE TO IT_NODETABLE. 179 180 END-OF-DEFINITION. 181 182 CREATE_TREE 'CN' '' '' 183 '' '' 'X' '' '中国'. 184 CREATE_TREE 'BEIJING' 'CN' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' 'X' '' '北京'. 185 CREATE_TREE 'FENGTAI' 'BEIJING' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6G@' '丰台区'. 186 CREATE_TREE 'HAIDIAN' 'BEIJING' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6G@' '海淀区'. 187 CREATE_TREE 'CHAOYANG' 'BEIJING' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6G@' '朝阳区'. 188 CREATE_TREE 'DONGCHENG' 'BEIJING' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6G@' '东城区'. 189 190 CREATE_TREE 'SHENGHAI' 'CN' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' 'X' '' '上海'. 191 CREATE_TREE 'QINGPU' 'SHENGHAI' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6F@' '青浦区'. 192 CREATE_TREE 'HONGKOU' 'SHENGHAI' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6F@' '虹口区'. 193 CREATE_TREE 'HUANGPU' 'SHENGHAI' CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD '' '' '' '@6F@' '黄浦区'. 194 195 * 为TREE对象添加节点 196 CALL METHOD WA_TREE->ADD_NODES 197 EXPORTING 198 TABLE_STRUCTURE_NAME = 'MTREESNODE' 199 NODE_TABLE = IT_NODETABLE 200 * EXCEPTIONS 201 * ERROR_IN_IT_NODETABLE = 1 202 * FAILED = 2 203 * DP_ERROR = 3 204 * TABLE_STRUCTURE_NAME_NOT_FOUND = 4 205 * others = 5 206 . 207 IF SY-SUBRC <> 0. 208 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 209 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 210 ENDIF. 211 212 *定义双击事件 213 EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK. 214 EVENT1-APPL_EVENT = 'X'. 215 APPEND EVENT1 TO EVENTS. 216 217 * 添加事件内表 218 CALL METHOD WA_TREE->SET_REGISTERED_EVENTS 219 EXPORTING 220 EVENTS = EVENTS 221 EXCEPTIONS 222 CNTL_ERROR = 1 223 CNTL_SYSTEM_ERROR = 2 224 ILLEGAL_EVENT_COMBINATION = 3 225 others = 4 226 . 227 IF SY-SUBRC <> 0. 228 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 229 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 230 ENDIF. 231 * 将已定义的双击事件分配到树对象 232 SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR WA_TREE. 233 ENDFORM. " FRM_CREATE_TREE
(二)建立有数据表接口的Tree Control 对象
从数据表中读取结构,并按照数据关系建立Tree Control,
提一下思路,不做具体事例。。。
将需要的表结构存储到内表中,然后LOOP这张内表,循环调用宏来填充节点内表:例如
DEFINE CREATE_TREE. CLEAR IW_NODETABLE. IW_NODETABLE-NODE_KEY = &1. "节点KEY值 IW_NODETABLE-RELATKEY = &2. "相关联的节点KEY值 IW_NODETABLE-RELATSHIP = &3. "关联关系 IW_NODETABLE-HIDDEN = &4. IW_NODETABLE-DISABLED = &5. IW_NODETABLE-ISFOLDER = &6. "文件夹形式 * 图标,可以运行程序 SHOWICON 查阅 IW_NODETABLE-N_IMAGE = &7. "图标 IW_NODETABLE-EXP_IMAGE = &7. "图标 * IW_NODETABLE-EXPANDER. IW_NODETABLE-TEXT = &8. "节点文本 APPEND IW_NODETABLE TO IT_NODETABLE. END-OF-DEFINITION. LOOP AT <ITAB> INTO <WA>. "循环调用宏 CREATE_TREE ' ' ' ' ' ' ' ' ' ' . ... ENDLOOP.
这样节点情况,也会根据数据情况来动态变化。
(三)通过右键菜单实现 Tree Control节点的 增 删 改 功能
在OO的类中,添加两个方法
*&---------------------------------------------------------------------* *& Class (DEFINITION) LCL_APPLICATION *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS LCL_APPLICATION DEFINITION. PUBLIC SECTION. METHODS: HANDLE_NODE_DOUBLE_CLICK FOR EVENT NODE_DOUBLE_CLICK OF CL_GUI_SIMPLE_TREE IMPORTING NODE_KEY, * 构成右键菜单的方法 HANDLE_NODE_CONTEXT_MENU_REQ FOR EVENT NODE_CONTEXT_MENU_REQUEST OF CL_GUI_SIMPLE_TREE IMPORTING NODE_KEY MENU, * 响应右键菜单的方法 HANDLE_NODE_CONTEXT_MENU_SEL FOR EVENT NODE_CONTEXT_MENU_SELECT OF CL_GUI_SIMPLE_TREE IMPORTING NODE_KEY FCODE. ENDCLASS. "LCL_APPLICATION
方法的实现
*&---------------------------------------------------------------------* *& Class (Implementation) LCL_APPLICATION *&---------------------------------------------------------------------* * Text *----------------------------------------------------------------------* CLASS LCL_APPLICATION IMPLEMENTATION. METHOD HANDLE_NODE_DOUBLE_CLICK. READ TABLE IT_NODETABLE WITH KEY NODE_KEY = NODE_KEY INTO IW_NODETABLE. L_KUNNRK = IW_NODETABLE-NODE_KEY. L_VTWEGK = IW_NODETABLE-RELATKEY. CLEAR IW_NODETABLE. READ TABLE IT_NODETABLE WITH KEY NODE_KEY = L_VTWEGK INTO IW_NODETABLE. L_VKORGK = IW_NODETABLE-RELATKEY. CLEAR IW_NODETABLE. PERFORM FRM_GET_DATA USING L_KUNNRK L_VTWEGK L_VKORGK. CALL METHOD WA_ALVG->REFRESH_TABLE_DISPLAY * EXPORTING * IS_STABLE = * I_SOFT_REFRESH = * EXCEPTIONS * FINISHED = 1 * others = 2 . IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. 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. READ TABLE IT_NODETABLE WITH KEY NODE_KEY = STR1 INTO IW_NODETABLE. IF FCODE = 'CODE4'. CALL METHOD WA_TREE->DELETE_NODE EXPORTING NODE_KEY = IW_NODETABLE-NODE_KEY . DELETE IT_NODETABLE WHERE NODE_KEY = IW_NODETABLE-NODE_KEY. ENDIF. ENDIF. ENDMETHOD. ENDCLASS. "LCL_APPLICATION
(四) Tree Control对象与ALV Grid对象关联
附,源代码:
1 *&---------------------------------------------------------------------* 2 *& Report ZHAIM_TREE_CONTROL 3 *& 4 *&---------------------------------------------------------------------* 5 *& 6 *& 7 *&---------------------------------------------------------------------* 8 9 REPORT ZHAIM_TREE_CONTROL. 10 * 功能码 11 DATA: OK_CODE TYPE SY-UCOMM, 12 SAVE_OK TYPE SY-UCOMM. 13 14 15 * 定制控制对象 16 DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'TREE', 17 WA_CONTAINERA TYPE SCRFNAME VALUE 'ALVG', 18 * 客户对象 19 WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER, 20 WA_CUSTOM_CNTNRALVG TYPE REF TO CL_GUI_CUSTOM_CONTAINER, 21 * 实例化对象 22 WA_TREE TYPE REF TO CL_GUI_SIMPLE_TREE, 23 WA_ALVG TYPE REF TO CL_GUI_ALV_GRID. 24 * 节点信息表 25 DATA: IT_NODETABLE TYPE STANDARD TABLE OF MTREESNODE, 26 IW_NODETABLE TYPE MTREESNODE. 27 28 DATA: IS_LAYOUT TYPE LVC_S_LAYO, 29 IT_FIELDCAT TYPE STANDARD TABLE OF LVC_S_FCAT, 30 IW_FIELDCAT LIKE LINE OF IT_FIELDCAT . 31 32 TYPES:BEGIN OF TY_VBAK , 33 VKORG TYPE VBAK-VKORG , 34 VTWEG TYPE VBAK-VTWEG , 35 KUNNR TYPE VBAK-KUNNR , 36 VBELN TYPE VBAK-VBELN , 37 VKORGT TYPE TVKOT-VTEXT, 38 VTWEGT TYPE TVTWT-VTEXT, 39 END OF TY_VBAK . 40 41 DATA: IT_VBAK TYPE STANDARD TABLE OF TY_VBAK, 42 IW_VBAK TYPE TY_VBAK. 43 44 TYPES: BEGIN OF TY_VBAP , 45 VBELN TYPE VBAP-VBELN , 46 POSNR TYPE VBAP-POSNR , 47 MATNR TYPE VBAP-MATNR , 48 NETWR TYPE VBAP-NETWR , 49 WERKS TYPE VBAP-WERKS , 50 LSMENG TYPE VBAP-LSMENG , 51 END OF TY_VBAP . 52 53 DATA: IT_VBAP TYPE STANDARD TABLE OF TY_VBAP, 54 IW_VBAP TYPE TY_VBAP. 55 56 DATA GS_LAYOUT TYPE LVC_S_LAYO. 57 DATA GT_FIELDCAT TYPE STANDARD TABLE OF LVC_S_FCAT . 58 DATA GW_FIELDCAT LIKE LINE OF GT_FIELDCAT . 59 * 通过宏定义树 节点 60 DEFINE CREATE_TREE. 61 62 CLEAR IW_NODETABLE. 63 64 IW_NODETABLE-NODE_KEY = &1. "节点KEY值 65 IW_NODETABLE-RELATKEY = &2. "相关联的节点KEY值 66 IW_NODETABLE-RELATSHIP = &3. "关联关系 67 IW_NODETABLE-HIDDEN = &4. 68 IW_NODETABLE-DISABLED = &5. 69 IW_NODETABLE-ISFOLDER = &6. "文件夹形式 70 * 图标,可以运行程序 SHOWICON 查阅 71 IW_NODETABLE-N_IMAGE = &7. "图标 72 IW_NODETABLE-EXP_IMAGE = &7. "图标 73 IW_NODETABLE-TEXT = &8. "节点文本 74 75 CLEAR IW_NODETABLE-EXPANDER. 76 77 APPEND IW_NODETABLE TO IT_NODETABLE. 78 79 END-OF-DEFINITION. 80 *创建一个类,实现一个双击方法 81 CLASS LCL_APPLICATION DEFINITION DEFERRED. 82 83 *&---------------------------------------------------------------------* 84 *& Class (DEFINITION) LCL_APPLICATION 85 *&---------------------------------------------------------------------* 86 * Text 87 *----------------------------------------------------------------------* 88 CLASS LCL_APPLICATION DEFINITION. 89 PUBLIC SECTION. 90 91 DATA: L_KUNNRK TYPE VBAK-KUNNR, 92 L_VTWEGK TYPE VBAK-VTWEG, 93 L_VKORGK TYPE VBAK-VKORG. 94 95 METHODS: 96 97 HANDLE_NODE_DOUBLE_CLICK 98 FOR EVENT NODE_DOUBLE_CLICK 99 OF CL_GUI_SIMPLE_TREE 100 IMPORTING NODE_KEY, 101 102 HANDLE_NODE_CONTEXT_MENU_REQ 103 FOR EVENT NODE_CONTEXT_MENU_REQUEST 104 OF CL_GUI_SIMPLE_TREE 105 IMPORTING NODE_KEY MENU, 106 107 HANDLE_NODE_CONTEXT_MENU_SEL 108 FOR EVENT NODE_CONTEXT_MENU_SELECT 109 OF CL_GUI_SIMPLE_TREE 110 IMPORTING NODE_KEY FCODE. 111 112 ENDCLASS. "LCL_APPLICATION 113 *&---------------------------------------------------------------------* 114 *& Class (Implementation) LCL_APPLICATION 115 *&---------------------------------------------------------------------* 116 * Text 117 *----------------------------------------------------------------------* 118 CLASS LCL_APPLICATION IMPLEMENTATION. 119 120 METHOD HANDLE_NODE_DOUBLE_CLICK. 121 READ TABLE IT_NODETABLE WITH KEY NODE_KEY = NODE_KEY INTO IW_NODETABLE. 122 L_KUNNRK = IW_NODETABLE-NODE_KEY. 123 L_VTWEGK = IW_NODETABLE-RELATKEY. 124 CLEAR IW_NODETABLE. 125 126 READ TABLE IT_NODETABLE WITH KEY NODE_KEY = L_VTWEGK INTO IW_NODETABLE. 127 L_VKORGK = IW_NODETABLE-RELATKEY. 128 CLEAR IW_NODETABLE. 129 130 PERFORM FRM_GET_DATA USING L_KUNNRK L_VTWEGK L_VKORGK. 131 132 CALL METHOD WA_ALVG->REFRESH_TABLE_DISPLAY 133 * EXPORTING 134 * IS_STABLE = 135 * I_SOFT_REFRESH = 136 * EXCEPTIONS 137 * FINISHED = 1 138 * others = 2 139 . 140 IF SY-SUBRC <> 0. 141 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 142 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 143 ENDIF. 144 145 146 ENDMETHOD. 147 148 METHOD HANDLE_NODE_CONTEXT_MENU_REQ. 149 CALL METHOD MENU->ADD_FUNCTION 150 EXPORTING 151 TEXT = '增加本级节点' 152 FCODE = 'CODE1'. 153 154 CALL METHOD MENU->ADD_FUNCTION 155 EXPORTING 156 TEXT = '增加子节点' 157 FCODE = 'CODE2'. 158 159 CALL METHOD MENU->ADD_FUNCTION 160 EXPORTING 161 TEXT = '修改当前节点' 162 FCODE = 'CODE3'. 163 164 CALL METHOD MENU->ADD_FUNCTION 165 EXPORTING 166 TEXT = '删除当前节点' 167 FCODE = 'CODE4'. 168 ENDMETHOD. 169 170 METHOD HANDLE_NODE_CONTEXT_MENU_SEL. 171 172 DATA STR1 TYPE TV_NODEKEY. 173 174 CALL METHOD WA_TREE->GET_SELECTED_NODE 175 IMPORTING 176 NODE_KEY = STR1. 177 178 IF SY-SUBRC = 0. 179 180 READ TABLE IT_NODETABLE WITH KEY NODE_KEY = STR1 INTO IW_NODETABLE. 181 182 183 IF FCODE = 'CODE4'. 184 CALL METHOD WA_TREE->DELETE_NODE 185 EXPORTING 186 NODE_KEY = IW_NODETABLE-NODE_KEY 187 . 188 DELETE IT_NODETABLE WHERE NODE_KEY = IW_NODETABLE-NODE_KEY. 189 ENDIF. 190 191 ENDIF. 192 193 ENDMETHOD. 194 ENDCLASS. "LCL_APPLICATION 195 196 * 双击方法的类的实例化对象 准备在PBO中分配给TREE对象 197 DATA: G_APPLICATION TYPE REF TO LCL_APPLICATION. 198 199 200 START-OF-SELECTION. 201 202 CREATE OBJECT G_APPLICATION. 203 PERFORM FRM_GET_NODE_DATA. 204 CALL SCREEN 100. 205 206 207 *&---------------------------------------------------------------------* 208 *& Module STATUS_0100 OUTPUT 209 *&---------------------------------------------------------------------* 210 * text 211 *----------------------------------------------------------------------* 212 MODULE STATUS_0100 OUTPUT. 213 SET PF-STATUS 'TREECTRL_GUIS'. 214 SET TITLEBAR 'TREE CONTROL'. 215 IF WA_CUSTOM_CONTAINER IS INITIAL. 216 PERFORM FRM_CREATE_TREE. 217 ENDIF. 218 ENDMODULE. " STATUS_0100 OUTPUT 219 220 *&---------------------------------------------------------------------* 221 *& Form FRM_CREATE_TREE 222 *&---------------------------------------------------------------------* 223 * text 224 *----------------------------------------------------------------------* 225 * --> p1 text 226 * <-- p2 text 227 *----------------------------------------------------------------------* 228 FORM FRM_CREATE_TREE . 229 DATA: EVENTS TYPE CNTL_SIMPLE_EVENTS, 230 EVENT1 TYPE CNTL_SIMPLE_EVENT. 231 232 * 定制控制对象,指定tree control的容器 233 CREATE OBJECT WA_CUSTOM_CONTAINER 234 EXPORTING 235 * PARENT = 236 CONTAINER_NAME = WA_CONTAINER " 定制对象名:TREE 237 * STYLE = 238 * LIFETIME = lifetime_default 239 * REPID = 240 * DYNNR = 241 * NO_AUTODEF_PROGID_DYNNR = 242 EXCEPTIONS 243 CNTL_ERROR = 1 244 CNTL_SYSTEM_ERROR = 2 245 CREATE_ERROR = 3 246 LIFETIME_ERROR = 4 247 LIFETIME_DYNPRO_DYNPRO_LINK = 5 248 OTHERS = 6 249 . 250 251 IF SY-SUBRC <> 0. 252 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 253 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 254 ENDIF. 255 256 * 定制控制对象,指定ALV 的容器 257 CREATE OBJECT WA_CUSTOM_CNTNRALVG 258 EXPORTING 259 * PARENT = 260 CONTAINER_NAME = WA_CONTAINERA 261 * STYLE = 262 * LIFETIME = lifetime_default 263 * REPID = 264 * DYNNR = 265 * NO_AUTODEF_PROGID_DYNNR = 266 * EXCEPTIONS 267 * CNTL_ERROR = 1 268 * CNTL_SYSTEM_ERROR = 2 269 * CREATE_ERROR = 3 270 * LIFETIME_ERROR = 4 271 * LIFETIME_DYNPRO_DYNPRO_LINK = 5 272 * others = 6 273 . 274 IF SY-SUBRC <> 0. 275 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 276 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 277 ENDIF. 278 279 280 281 * 定义树对象 282 CREATE OBJECT WA_TREE 283 EXPORTING 284 * LIFETIME = 285 PARENT = WA_CUSTOM_CONTAINER 286 * SHELLSTYLE = 287 NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE 288 * HIDE_SELECTION = 289 * NAME = 290 EXCEPTIONS 291 LIFETIME_ERROR = 1 292 CNTL_SYSTEM_ERROR = 2 293 CREATE_ERROR = 3 294 FAILED = 4 295 ILLEGAL_NODE_SELECTION_MODE = 5 296 others = 6 297 . 298 IF SY-SUBRC <> 0. 299 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 300 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 301 ENDIF. 302 303 * 定义ALV对象 304 305 CREATE OBJECT WA_ALVG 306 EXPORTING 307 * I_SHELLSTYLE = 0 308 * I_LIFETIME = 309 I_PARENT = WA_CUSTOM_CNTNRALVG 310 * I_APPL_EVENTS = space 311 * I_PARENTDBG = 312 * I_APPLOGPARENT = 313 * I_GRAPHICSPARENT = 314 * I_NAME = 315 * I_FCAT_COMPLETE = SPACE 316 * EXCEPTIONS 317 * ERROR_CNTL_CREATE = 1 318 * ERROR_CNTL_INIT = 2 319 * ERROR_CNTL_LINK = 3 320 * ERROR_DP_CREATE = 4 321 * others = 5 322 . 323 IF SY-SUBRC <> 0. 324 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 325 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 326 ENDIF. 327 328 DATA: L_VKORGT TYPE TVKOT-VTEXT, 329 L_VTWEGT TYPE TVTWT-VTEXT, 330 L_VKORG TYPE VBAK-VKORG, 331 L_VTWEG TYPE VBAK-VTWEG, 332 L_KUNNR TYPE VBAK-KUNNR. 333 334 335 LOOP AT IT_VBAK INTO IW_VBAK. 336 L_VKORG = IW_VBAK-VKORG. 337 L_VTWEG = IW_VBAK-VTWEG. 338 L_KUNNR = IW_VBAK-KUNNR. 339 L_VKORGT = IW_VBAK-VKORGT. 340 L_VTWEGT = IW_VBAK-VTWEGT. 341 342 CREATE_TREE L_VKORG '' '' '' '' 'X' '' L_VKORGT. 343 344 CREATE_TREE L_VTWEG L_VKORG 6 '' '' 'X' '' L_VTWEGT. 345 346 CREATE_TREE L_KUNNR L_VTWEG 6 '' '' '' '@5B@' IW_VBAK-KUNNR. 347 348 CLEAR: L_VKORGT, 349 L_VTWEGT, 350 L_VKORG, 351 L_VTWEG, 352 L_KUNNR, 353 IW_VBAK. 354 ENDLOOP. 355 356 * 为TREE对象添加节点 357 SORT IT_NODETABLE BY NODE_KEY RELATKEY. 358 DELETE ADJACENT DUPLICATES FROM IT_NODETABLE COMPARING NODE_KEY RELATKEY. 359 360 SORT IT_NODETABLE BY RELATSHIP ASCENDING ISFOLDER DESCENDING. 361 CALL METHOD WA_TREE->ADD_NODES 362 EXPORTING 363 TABLE_STRUCTURE_NAME = 'MTREESNODE' 364 NODE_TABLE = IT_NODETABLE 365 * EXCEPTIONS 366 * ERROR_IN_IT_NODETABLE = 1 367 * FAILED = 2 368 * DP_ERROR = 3 369 * TABLE_STRUCTURE_NAME_NOT_FOUND = 4 370 * others = 5 371 . 372 IF SY-SUBRC <> 0. 373 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 374 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 375 ENDIF. 376 377 PERFORM FRM_SET_ALVTB . 378 379 *定义双击事件 380 EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK. 381 EVENT1-APPL_EVENT = 'X'. 382 APPEND EVENT1 TO EVENTS. 383 384 EVENT1-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_CONTEXT_MENU_REQ. 385 EVENT1-APPL_EVENT = ''. 386 APPEND EVENT1 TO EVENTS. 387 388 CALL METHOD WA_TREE->SET_CTX_MENU_SELECT_EVENT_APPL 389 EXPORTING 390 APPL_EVENT = 'X' . 391 392 * 添加事件内表 393 CALL METHOD WA_TREE->SET_REGISTERED_EVENTS 394 EXPORTING 395 EVENTS = EVENTS 396 EXCEPTIONS 397 CNTL_ERROR = 1 398 CNTL_SYSTEM_ERROR = 2 399 ILLEGAL_EVENT_COMBINATION = 3 400 others = 4 401 . 402 IF SY-SUBRC <> 0. 403 * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 404 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 405 ENDIF. 406 * 将已定义的双击事件分配到树对象 407 SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR WA_TREE. 408 SET HANDLER G_APPLICATION->HANDLE_NODE_CONTEXT_MENU_REQ FOR WA_TREE. 409 SET HANDLER G_APPLICATION->HANDLE_NODE_CONTEXT_MENU_SEL FOR WA_TREE. 410 ENDFORM. " FRM_CREATE_TREE 411 412 *&---------------------------------------------------------------------* 413 *& Module USER_COMMAND_0100 INPUT 414 *&---------------------------------------------------------------------* 415 * text 416 *----------------------------------------------------------------------* 417 MODULE USER_COMMAND_0100 INPUT. 418 419 SAVE_OK = OK_CODE. 420 421 CLEAR OK_CODE. 422 423 CASE SAVE_OK. 424 WHEN 'BACK'. 425 LEAVE PROGRAM. 426 WHEN OTHERS. 427 ENDCASE. 428 429 ENDMODULE. " USER_COMMAND_0100 INPUT 430 *&---------------------------------------------------------------------* 431 *& Form FRM_GET_DATA 432 *&---------------------------------------------------------------------* 433 * text 434 *----------------------------------------------------------------------* 435 * -->P_NODE_KEY text 436 * -->P_IW_NODETABLE_RELATKEY text 437 *----------------------------------------------------------------------* 438 FORM FRM_GET_DATA USING P_KUNNR 439 P_VTWEG 440 P_VKORG. 441 SELECT 442 VBAP~VBELN 443 VBAP~POSNR 444 VBAP~MATNR 445 VBAP~NETWR 446 VBAP~WERKS 447 VBAP~LSMENG 448 INTO TABLE IT_VBAP 449 FROM VBAP 450 INNER JOIN VBAK ON VBAP~VBELN = VBAK~VBELN 451 WHERE VBAK~VKORG = P_VKORG 452 AND VBAK~VTWEG = P_VTWEG 453 AND VBAK~KUNNR = P_KUNNR 454 AND VBAK~AUART NE 'Z001'. 455 IF IT_VBAP IS INITIAL. 456 MESSAGE S001(00) WITH '数据不存在或者尝试选择下级子节点'. 457 LEAVE TO SCREEN 100. 458 ENDIF. 459 460 LOOP AT IT_VBAP INTO IW_VBAP. 461 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' 462 EXPORTING 463 INPUT = IW_VBAP-VBELN 464 IMPORTING 465 OUTPUT = IW_VBAP-VBELN. 466 467 CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' 468 EXPORTING 469 INPUT = IW_VBAP-MATNR 470 IMPORTING 471 OUTPUT = IW_VBAP-MATNR. 472 MODIFY IT_VBAP FROM IW_VBAP . 473 ENDLOOP. 474 475 ENDFORM. " GET_DATA 476 *&---------------------------------------------------------------------* 477 *& Form FRM_GET_NODE_DATA 478 *&---------------------------------------------------------------------* 479 * text 480 *----------------------------------------------------------------------* 481 * --> p1 text 482 * <-- p2 text 483 *----------------------------------------------------------------------* 484 FORM FRM_GET_NODE_DATA . 485 486 SELECT A~VKORG 487 A~VTWEG 488 A~KUNNR 489 A~VBELN 490 B~VTEXT 491 C~VTEXT 492 INTO TABLE IT_VBAK 493 FROM VBAK AS A 494 INNER JOIN TVKOT AS B ON B~VKORG = A~VKORG 495 INNER JOIN TVTWT AS C ON C~VTWEG = A~VTWEG 496 WHERE B~SPRAS = '1' AND C~SPRAS = '1'. 497 498 SORT IT_VBAK BY VKORG VTWEG KUNNR VBELN. 499 DELETE ADJACENT DUPLICATES FROM IT_VBAK COMPARING VKORG VTWEG KUNNR. 500 501 IF SY-SUBRC <> 0. 502 503 ENDIF. 504 505 ENDFORM. " FRM_GET_NODE_DATA 506 *&---------------------------------------------------------------------* 507 *& Form FRM_SET_ALVTB 508 *&---------------------------------------------------------------------* 509 * text 510 *----------------------------------------------------------------------* 511 * --> p1 text 512 * <-- p2 text 513 *----------------------------------------------------------------------* 514 FORM FRM_SET_ALVTB . 515 516 CLEAR GW_FIELDCAT . 517 GW_FIELDCAT-FIELDNAME = 'VBELN' . 518 GW_FIELDCAT-SCRTEXT_L = '销售订单' . 519 APPEND GW_FIELDCAT TO GT_FIELDCAT . 520 521 CLEAR GW_FIELDCAT . 522 GW_FIELDCAT-FIELDNAME = 'POSNR' . 523 GW_FIELDCAT-SCRTEXT_L = '销售订单项目' . 524 APPEND GW_FIELDCAT TO GT_FIELDCAT . 525 526 CLEAR GW_FIELDCAT . 527 GW_FIELDCAT-FIELDNAME = 'MATNR' . 528 GW_FIELDCAT-SCRTEXT_L = '物料编码' . 529 APPEND GW_FIELDCAT TO GT_FIELDCAT . 530 531 CLEAR GW_FIELDCAT . 532 GW_FIELDCAT-FIELDNAME = 'NETWR' . 533 GW_FIELDCAT-SCRTEXT_L = '净价' . 534 APPEND GW_FIELDCAT TO GT_FIELDCAT . 535 536 CLEAR GW_FIELDCAT . 537 GW_FIELDCAT-FIELDNAME = 'WERKS' . 538 GW_FIELDCAT-SCRTEXT_L = '工厂' . 539 APPEND GW_FIELDCAT TO GT_FIELDCAT . 540 541 CLEAR GW_FIELDCAT . 542 GW_FIELDCAT-FIELDNAME = 'LSMENG' . 543 GW_FIELDCAT-SCRTEXT_L = '销售订单数量' . 544 APPEND GW_FIELDCAT TO GT_FIELDCAT . 545 546 GS_LAYOUT-GRID_TITLE = 'SD分销渠道销售订单数据' . 547 "GS_LAYOUT-CWIDTH_OPT = 'X' . 548 GS_LAYOUT-ZEBRA = 'X' . 549 550 CALL METHOD WA_ALVG->SET_TABLE_FOR_FIRST_DISPLAY 551 EXPORTING 552 IS_LAYOUT = GS_LAYOUT 553 CHANGING 554 IT_FIELDCATALOG = GT_FIELDCAT 555 IT_OUTTAB = IT_VBAP. 556 557 ENDFORM. " FRM_SET_ALVTB