先看效果图:
用cl_gui_alv_tree_simple 这个类
下面直接上干货:
首先
建一个屏幕 9001
画一个容器,取名:TREE
定义数据:
*功能码返回值
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm.
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: gb_fieldcat TYPE lvc_t_fcat. "控制输出列
DATA: gb_sortfld TYPE lvc_t_sort. "控制节点
"定义事件注册类(使用双击事件)
*----------------------------------------------------------------------*
* INCLUDE BCALV_TREE_EVENT_REC_DRAGDROP *
*----------------------------------------------------------------------*
CLASS cl_tree_event_receiver DEFINITION.
PUBLIC SECTION.
* double click item
METHODS handle_double_click
FOR EVENT node_double_click OF cl_gui_alv_tree_simple
IMPORTING index_outtab
grouplevel.
ENDCLASS. "CL_TREE_EVENT_RECEIVER DEFINITION
*---------------------------------------------------------------------*
* CLASS CL_TREE_EVENT_RECEIVER IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS cl_tree_event_receiver IMPLEMENTATION.
METHOD handle_double_click.
CHECK NOT index_outtab IS INITIAL.
"获取当前行
READ TABLE gt_show(数据内表) INTO l_wa_data INDEX index_outtab.
if sy-subrc = 0.
"事件操作
endif.
ENDMETHOD. "HANDLE_DOUBLE_CLICK
ENDCLASS. "CL_TREE_EVENT_RECEIVER IMPLEMENTATION
下面是创建树的过程:
FORM zform_create_tree.
IF wa_custom_container_tree IS INITIAL.
"事件相关
DATA: tree_event_receiver TYPE REF TO cl_tree_event_receiver.
CREATE OBJECT tree_event_receiver.
"报表头
DATA ls_list_comm TYPE slis_t_listheader.
DATA ls_alist_comm TYPE slis_listheader.
ls_alist_comm-typ = 'H'.
ls_alist_comm-info = '报表'.
APPEND ls_alist_comm TO ls_list_comm.
"填充输出结构
PERFORM zform_fieldcat.
"控制输出层次
PERFORM zform_field_level.
CREATE OBJECT wa_custom_container_tree
EXPORTING
container_name = wa_container_tree.
CREATE OBJECT tree_grid
EXPORTING
i_parent = wa_custom_container_tree.
CALL METHOD tree_grid->set_table_for_first_display
EXPORTING
it_list_commentary = ls_list_comm
" i_structure_name = 'ZTEST_SHOW' "se11建的想要展示的结构
CHANGING
it_sort = gb_sortfld
it_fieldcatalog = gb_fieldcat
it_outtab = gt_show[]. "数据表
CALL METHOD tree_grid->expand_tree
EXPORTING
i_level = 1.
"调用事件
** register events
PERFORM register_events.
** set handler for tree1
SET HANDLER tree_event_receiver->handle_double_click FOR tree_grid.
ENDIF.
ENDFORM.
控制输出字段
FORM zform_fieldcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'ZTEST_BJH_CHECK_SHOW1'
CHANGING
ct_fieldcat = gb_fieldcat.
DATA ls_fldcat TYPE lvc_s_fcat.
LOOP AT gb_fieldcat INTO ls_fldcat.
ls_fldcat-hotspot = 'X'.
CASE ls_fldcat-fieldname.
WHEN
字段某某
ls_fldcat-outputlen = 15. "设置长度是15
WHEN OTHERS.
ls_fldcat-no_out = 'X'. "不需要显示的设置为不输出
ENDCASE.
IF ls_fldcat-fieldname = 'TSLVT'. "对TSLVT(金额)字段进行求和
* ls_fldcat-do_sum = 'X'.
ENDIF.
MODIFY gb_fieldcat FROM ls_fldcat.
ENDLOOP.
ENDFORM. "zform_fieldcat
"控制展现层次(第一层是地区,第二层是上级科目,第三层是目标科目
FORM zform_field_level.
DATA:
l_index TYPE int4 VALUE '1'.
DATA ls_sortfld TYPE lvc_s_sort.
ls_sortfld-spos = l_index.
ls_sortfld-fieldname = 'QYDM'. "区域 "
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
l_index = l_index + 1.
ls_sortfld-spos = l_index.
ls_sortfld-fieldname = 'HKONT_PREV'. "上级科目编号 "
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
l_index = l_index + 1.
ls_sortfld-spos = l_index.
ls_sortfld-fieldname = 'HKONT'. "科目编号 "
ls_sortfld-up = 'X'.
ls_sortfld-subtot = 'X'.
APPEND ls_sortfld TO gb_sortfld.
l_index = l_index + 1.
ENDFORM. "zform_field_level