效果图:
干货代码:
首先创建屏幕,
画个容器,命名TREE
DATA:
wa_container_tree TYPE scrfname VALUE \’TREE\’, \”*定制对象
wa_custom_container_tree TYPE REF TO cl_gui_custom_container, \”*客户对象
tree_grid TYPE REF TO cl_gui_alv_tree_simple,
DATA: node_table LIKE TABLE OF mtreesnode, \”NODE 节点表
node_wa TYPE mtreesnode. \”节点
\”取完数,后显示form
FORM zform_show .
PERFORM zform_create_tree. \”创建树
PERFORM zform_create_nodes. \”创建节点
PERFORM zform_add_nodes. \”添加节点
ENDFORM. \” ZFORM_SHOW
\”1、创建tree object
FORM zform_create_tree.
*建立定制控制对象
CREATE OBJECT wa_custom_container_tree
EXPORTING
container_name = wa_container_tree.
*建立树对象
CREATE OBJECT tree_grid2
EXPORTING
* LIFETIME =
parent = wa_custom_container_tree
* SHELLSYLE =
node_selection_mode = cl_gui_simple_tree=>node_sel_mode_single
* HIDE_SELECTION =
* NAME =
EXCEPTIONS
lifetime_error = 1
cntl_system_error = 2
create_error = 3
failed = 4
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. \”zform_create_tree
\”2、创建节点
*&———————————————————————*
* 2.1、创建头节点
*———————————————————————-*
FORM zform_create_head_node USING u_head_node \”头节点值
u_text . \”节点文本
CLEAR node_wa.
*节点关键字
node_wa-node_key = u_head_node.
*第一层
CLEAR node_wa-relatkey.
CLEAR node_wa-relatship.
*是文件夹
node_wa-hidden = \’\’.
node_wa-disabled = \’\’.
node_wa-isfolder = \’X\’.
*不带ICON
CLEAR node_wa-n_image.
CLEAR node_wa-exp_image.
CLEAR node_wa-expander.
node_wa-text = u_text.
APPEND node_wa TO node_table.
ENDFORM. \”zform_create_head_node
*&———————————————————————*
* 2.2 建末节点(叶子节点)
*———————————————————————-*
FORM zform_create_last_node USING u_node_key \”节点值
u_relatkey \”上层节点
t_text . \”节点文本
CLEAR node_wa.
node_wa-node_key = u_node_key.
*上层关系
node_wa-relatkey = u_relatkey.
node_wa-relatship = cl_gui_simple_tree=>relat_last_child.
node_wa-hidden = \’\’.
node_wa-disabled = \’\’.
node_wa-isfolder = space. \”非文件夹
CLEAR node_wa-n_image.
CLEAR node_wa-exp_image.
CLEAR node_wa-expander.
node_wa-text = t_text.
APPEND node_wa TO node_table.
ENDFORM. \”zform_create_last_node
*&———————————————————————*
* 2.3、创建中间节点(文件夹)
*———————————————————————-*
FORM zform_create_folde_node USING u_node_key
u_relatkey
t_text .
CLEAR node_wa.
node_wa-node_key = u_node_key.
*上层关系
node_wa-relatkey = u_relatkey.
node_wa-relatship = cl_gui_simple_tree=>relat_last_child.
node_wa-hidden = \’\’.
node_wa-disabled = \’\’.
node_wa-isfolder = \’X\’.
CLEAR node_wa-n_image.
CLEAR node_wa-exp_image.
CLEAR node_wa-expander.
node_wa-text = t_text.
APPEND node_wa TO node_table.
ENDFORM. \”zform_create_last_node
\”根据数据结构,填充各节点数据
*&———————————————————————*
*& Form zform_create_nodes
*&———————————————————————*
* text 创建节点内表
*———————————————————————-*
FORM zform_create_nodes.
DATA: l_wa_data LIKE LINE OF g_kemu_show.
CLEAR l_wa_data.
LOOP AT g_kemu_show INTO l_wa_data.
IF l_wa_data-level_key = 1. \”等级标志1头节点,2中间节点,9叶子节点
PERFORM zform_create_head_node
USING l_wa_data-bjh_hkont
l_wa_data-bjh_hkont.
ELSEIF l_wa_data-level_key = 9. \”叶子节点
PERFORM zform_create_last_node
USING l_wa_data-bjh_hkont
l_wa_data-bjh_hkont_prev
l_wa_data-bjh_hkont.
ELSE.
PERFORM zform_create_folde_node \”中间节点(文件夹)
USING l_wa_data-bjh_hkont
l_wa_data-bjh_hkont_prev
l_wa_data-bjh_hkont.
ENDIF.
CLEAR l_wa_data.
ENDLOOP.
ENDFORM. \”zform_create_nodes
*&———————————————————————*
* 3、最后添加节点到树
*———————————————————————-*
FORM zform_add_nodes.
*按照节点内容添加节点
CALL METHOD tree_grid->add_nodes
EXPORTING
table_structure_name = \’MTREESNODE\’ \”MTREESNODE 此结构是固定的:node_table类型mtreesnode
node_table = node_table
* EXCEPTIONS
* ERROR_IN_NODE_TABLE = 1
* FAILED = 2
* DP_ERROF = 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.
ENDFORM. \”zform_add_nodes