SAP屏幕设计器专题:树控件的使用(九)
在SAP的标准画面中,树控件都是随处可见,在一些特殊的应用中更有用处, 显得专业。
在SE51设计界面里,并没有直观的树控件,只是一个容器,要实现树功能还得在程序中用面向对象的方法实现。
本文就简要介绍一下应用。
定义部分:
*内表部分
DATA:IT_TREE LIKE STANDARD TABLE OF ZOA_REG WITH HEADER LINE.
DATA IT_TREE1 LIKE IT_TREE.
FIELD-SYMBOLS <FS1> LIKE LINE OF IT_TREE.
* ----->TREE
CLASS LCL_APPLICATION DEFINITION DEFERRED.
CLASS CL_GUI_CFW DEFINITION LOAD.
TYPES: NODE_TABLE_TYPE LIKE STANDARD TABLE OF MTREESNODE
WITH DEFAULT KEY.
DATA:NODE_TABLE TYPE NODE_TABLE_TYPE .
* CAUTION: MTREESNODE IS THE NAME OF THE NODE STRUCTURE WHICH MUST
* BE DEFINED BY THE PROGRAMMER. DO NOT USE MTREESNODE!
DATA: G_APPLICATION TYPE REF TO LCL_APPLICATION,
G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
G_TREE TYPE REF TO CL_GUI_SIMPLE_TREE.
DATA: G_EVENT(30),
G_NODE_KEY TYPE TV_NODEKEY.
*文本控件
DATA:USERNO(20),
USERNAME(20),
PASSWORD(20),
BRANCH(20),
UNIT(20),
MAIL(20),
TDATE(20),
SEX(20),
NOQUIT,
QUIT.
2、状态及功能
*&---------------------------------------------------------------------*
*& MODULE STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'PFSTATUS'.
SET TITLEBAR 'TITLE'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& MODULE USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE SY-UCOMM.
WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
LEAVE.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
3、定义双击事件的定义和方法体:
*----------------------------------------------------------------------*
* CLASS LCL_APPLICATION DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_APPLICATION DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_NODE_DOUBLE_CLICK
FOR EVENT NODE_DOUBLE_CLICK
OF CL_GUI_SIMPLE_TREE
IMPORTING NODE_KEY.
ENDCLASS. "LCL_APPLICATION DEFINITION
*----------------------------------------------------------------------*
* CLASS LCL_APPLICATION IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_APPLICATION IMPLEMENTATION.
METHOD HANDLE_NODE_DOUBLE_CLICK.
FIELD-SYMBOLS : <NODE> TYPE mtreesnode.
" THIS METHOD HANDLES THE NODE DOUBLE CLICK EVENT OF THE TREE
" CONTROL INSTANCE
READ TABLE NODE_TABLE ASSIGNING <NODE>
WITH KEY NODE_KEY = NODE_KEY .
IF NODE_KEY CS 'ZOA_REG'. "點擊父節點不做處理
MESSAGE <NODE>-TEXT TYPE 'I'.
ENDIF.
READ TABLE IT_TREE INTO IT_TREE1 WITH KEY USERNO = <NODE>-TEXT.
IF SY-SUBRC = 0.
USERNO = IT_TREE1-USERNO.
"USERNAME = IT_TREE1-NAME.
PASSWORD = IT_TREE1-PASS.
BRANCH = IT_TREE1-BRANCH.
UNIT = IT_TREE1-UNIT.
MAIL = IT_TREE1-MAIL.
TDATE = IT_TREE1-TDATE.
SEX = IT_TREE1-SEX.
IF IT_TREE1-STATE = '在职'.
NOQUIT = 'X'.
QUIT = ''.
ELSE.
NOQUIT = ''.
QUIT = 'X'.
ENDIF.
ELSE.
MESSAGE NODE_KEY TYPE 'I'.
ENDIF.
ENDMETHOD. "HANDLE_NODE_DOUBLE_CLICK
ENDCLASS. "LCL_APPLICATION IMPLEMENTATION
4、定义填充TREE控件的节点
*&---------------------------------------------------------------------*
*& FORM GET_PRO_ITEM
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM GET_PRO_ITEM .
PERFORM GET_DATA .
IF G_TREE IS INITIAL.
PERFORM CREATE_AND_INIT_TREE.
ENDIF.
ENDFORM. " GET_PRO_ITEM
*&---------------------------------------------------------------------*
*& FORM CREATE_AND_INIT_TREE
*&---------------------------------------------------------------------*
* 初始化树
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM CREATE_AND_INIT_TREE .
DATA: EVENTS TYPE CNTL_SIMPLE_EVENTS,
EVENT TYPE CNTL_SIMPLE_EVENT.
* CREATE A CONTAINER FOR THE TREE CONTROL
CREATE OBJECT G_CUSTOM_CONTAINER
EXPORTING " THE CONTAINER IS LINKED TO THE CUSTOM CONTROL WITH THE
" NAME 'TREE_CONTAINER' ON THE DYNPRO
CONTAINER_NAME = 'THE_TREE' "填充到画面上的容器里
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 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.
* CREATE A TREE CONTROL
CREATE OBJECT G_TREE
EXPORTING
PARENT = G_CUSTOM_CONTAINER
NODE_SELECTION_MODE = CL_GUI_SIMPLE_TREE=>NODE_SEL_MODE_SINGLE "单选模式
EXCEPTIONS
LIFETIME_ERROR = 1" SINGLE NODE SELECTIONIS USED
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
FAILED = 4
ILLEGAL_NODE_SELECTION_MODE = 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.
* DEFINE THE EVENTS WHICH WILL BE PASSED TO THE BACKEND
" NODE DOUBLE CLICK
EVENT-EVENTID = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK. "定义双击事件
EVENT-APPL_EVENT = 'X'. " PROCESS PAI IF EVENT OCCURS
APPEND EVENT TO EVENTS.
CALL METHOD G_TREE->SET_REGISTERED_EVENTS
EXPORTING
EVENTS = EVENTS
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
ILLEGAL_EVENT_COMBINATION = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* ASSIGN EVENT HANDLERS IN THE APPLICATION CLASS TO EACH DESIRED EVENT
CREATE OBJECT G_APPLICATION.
SET HANDLER G_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR G_TREE. "实现双击
PERFORM BUILD_NODE_TABLE USING NODE_TABLE. "填充树的节点
CALL METHOD G_TREE->ADD_NODES
EXPORTING
TABLE_STRUCTURE_NAME = 'MTREESNODE'
NODE_TABLE = NODE_TABLE
EXCEPTIONS
FAILED = 1
ERROR_IN_NODE_TABLE = 2
DP_ERROR = 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.
*設置TREE CONTROL中的節點默認為展開狀態
CALL METHOD G_TREE->EXPAND_NODE
EXPORTING
NODE_KEY = G_NODE_KEY
* LEVEL_COUNT =
* EXPAND_SUBTREE =
EXCEPTIONS
FAILED = 1
ILLEGAL_LEVEL_COUNT = 2
CNTL_SYSTEM_ERROR = 3
NODE_NOT_FOUND = 4
CANNOT_EXPAND_LEAF = 5
OTHERS = 6.
IF SY-SUBRC <> 0."...
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " CREATE_AND_INIT_TREE
*&---------------------------------------------------------------------*
*& FORM BUILD_NODE_TABLE
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->P_NODE_TABLE TEXT
*----------------------------------------------------------------------*
FORM BUILD_NODE_TABLE USING NODE_TABLE TYPE NODE_TABLE_TYPE.
DATA:NODE TYPE MTREESNODE.
DATA: L_INDEX LIKE SY-TABIX.
**設置父節點屬性
NODE-NODE_KEY = 'ZOA_REG'.
G_NODE_KEY = NODE-NODE_KEY.
CLEAR NODE-RELATKEY. " SPECIAL CASE: A ROOT NODE HAS NO PARENT
CLEAR NODE-RELATSHIP.
NODE-HIDDEN = ''. " THE NODE IS VISIBLE,
NODE-DISABLED = ''. " SELECTABLE,
NODE-ISFOLDER = 'X'. " A FOLDER.
* NODE-N_IMAGE = 'X'.
* NODE-EXP_IMAGE = 'X'.
NODE-STYLE = CL_GUI_SIMPLE_TREE=>STYLE_DEFAULT.
* NODE-NO_BRANCH = 'X'.
* NODE-EXPANDER = 'X'.
* NODE-DRAGDROPID
NODE-TEXT = '员工工号如下'.
APPEND NODE TO NODE_TABLE.
**設置子節點屬性
SORT IT_TREE BY USERNO.
L_INDEX = 0.
LOOP AT IT_TREE ASSIGNING <FS1>.
CLEAR: NODE-N_IMAGE.
L_INDEX = L_INDEX + 1.
NODE-NODE_KEY = L_INDEX .
NODE-RELATKEY = 'ZOA_REG'.
NODE-RELATSHIP = CL_GUI_SIMPLE_TREE=>RELAT_LAST_CHILD.
NODE-HIDDEN = ''. " THE NODE IS VISIBLE,
NODE-DISABLED = ''.
NODE-ISFOLDER = ' '. " 'X' 表示可折叠;'':表示不可折叠
NODE-TEXT = <FS1>-USERNO.
APPEND NODE TO NODE_TABLE.
ENDLOOP.
ENDFORM. " BUILD_NODE_TABLE
*&---------------------------------------------------------------------*
*& FORM GET_DATA
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->P_R_DATE TEXT
*----------------------------------------------------------------------*
FORM GET_DATA .
CLEAR : IT_TREE[].
SELECT * FROM ZOA_REG
INTO CORRESPONDING FIELDS OF TABLE IT_TREE WHERE WERKS = '1201'.
IF IT_TREE[] IS NOT INITIAL.
ELSE.
MESSAGE 'ERROR' TYPE 'E'.
ENDIF.
ENDFORM. " GET_DATA
5、画面SE51中设置如下图:
6、数据表如下:
7、运行结果:
至此,树控件设置结束。
-----------------------------------------------------------------------------------------------------------------------------
SAP ALL进行时...!
注:本文系原创,如要转载请务必保持原文一致并注明作者(SAP梦心)及出处(博客地址:http://www.cnblogs.com/saper/),违者将会被追究相关责任,谢谢!