码迷,mamicode.com
首页 > 其他好文 > 详细

实例:ABAP Tree Control 使用

时间:2015-12-15 18:03:19      阅读:624      评论:0      收藏:0      [点我收藏+]

标签:

Tree Control 是最常用的Windows控件之一,在其他语言中成为"Tree View"等,ABAP的 Tree Contiol 能实现类似的功能,

下面实例一个简单的 Tree Control.

技术分享

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

 

实例:ABAP Tree Control 使用

标签:

原文地址:http://www.cnblogs.com/mingdashu/p/treecontrol.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!