SAP ABAP ALV创建动态树形菜单
创建动态树形菜单——ALV
创建的合同越多,使用树形菜单能比较直观的地显示,而且展开下阶也能明确的知识相关的信息,比如合同中的出口成品有哪些。
设计要点:
第一、 Node_key一定要区分,不能重复,否则出错;
第二、 每一级别,其node_key编制有规律性;这样才能处理复杂的事件响应;
第三、 四层级别,下一级别包括上一级别的相关信息,在创建内表时可能包括;比如子类可以拥有父类的一些信息,孙类级别则包含子类以上的相关信息。这些信息为后续数据处理更直接更方便的提供了需要的信息。
代码设计:
*编制树节点及项目
DATA: BEGIN OF root OCCURS 0 ,
rkey TYPE string,
rtext TYPE string,
rname TYPE char1,
rcount TYPE string,
rhname LIKE zsd19cont-hname,
END OF root.
DATA: BEGIN OF child OCCURS 0.
INCLUDE STRUCTURE root.
DATA:ckey TYPE string,
ctext TYPE string,
cname TYPE char1,
ccount TYPE string.
DATA: END OF child.
DATA: BEGIN OF grandson OCCURS 0.
INCLUDE STRUCTURE child.
DATA:gkey TYPE string,
gtext TYPE string,
gname TYPE char1,
gcount TYPE string.
DATA: END OF grandson.
DATA: BEGIN OF nitem OCCURS 0.
INCLUDE STRUCTURE grandson.
DATA:nkey TYPE string,
ntext TYPE string,
nname TYPE char1,
ncount TYPE string.
DATA: END OF nitem.
*&---------------------------------------------------------------------*
*& Form set_root_info
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_root_info.
CLEAR t_cont[].
CLEAR root[].
SELECT * FROM zsd19cont INTO CORRESPONDING FIELDS OF TABLE t_cont WHERE hname IN s_hname AND lvorm <> 'X'.
LOOP AT t_cont.
count = sy-tabix.
CONCATENATE 'r' count INTO root-rkey.
CONCATENATE '合同' t_cont-sname INTO root-rtext.
CONDENSE root-rkey NO-GAPS.
root-rname = '1'.
root-rcount = count.
root-rhname = t_cont-hname.
APPEND root.
CLEAR root.
ENDLOOP.
* root-rkey = 'r2'.
* root-rtext = '合同-2009-138'.
* root-rname = '1'.
* root-rcount = 2.
* append root.
* clear root.
ENDFORM. "set_root_info
*&---------------------------------------------------------------------*
*& Form set_child_info
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_child_info.
CLEAR child[].
LOOP AT root.
MOVE-CORRESPONDING root TO child .
CONCATENATE root-rcount '1' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'c' count INTO child-ckey.
CONDENSE child-ckey NO-GAPS.
child-ctext = '商品信息'.
child-cname = '1'.
child-ccount = count .
APPEND child.
CLEAR child.
MOVE-CORRESPONDING root TO child .
CONCATENATE root-rcount '2' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'c' count INTO child-ckey.
CONDENSE child-ckey NO-GAPS.
child-ctext = '报关信息'.
child-cname = '1'.
child-ccount = count .
APPEND child.
CLEAR child.
ENDLOOP.
ENDFORM. "set_child_info
*&---------------------------------------------------------------------*
*& Form set_grandson_info
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_grandson_info.
CLEAR grandson[].
LOOP AT child.
IF child-ctext = '商品信息'.
MOVE-CORRESPONDING child TO grandson .
CONCATENATE child-ccount '1' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'g' count INTO grandson-gkey.
CONDENSE grandson-gkey NO-GAPS.
grandson-gtext = '出口成品→维护'.
grandson-gname = '1'.
grandson-gcount = count .
APPEND grandson.
CLEAR grandson.
MOVE-CORRESPONDING child TO grandson .
CONCATENATE child-ccount '2' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'g' count INTO grandson-gkey.
CONDENSE grandson-gkey NO-GAPS.
grandson-gtext = '进口料件→汇总'.
grandson-gname = '1'.
grandson-gcount = count .
APPEND grandson.
CLEAR grandson.
ELSE.
MOVE-CORRESPONDING child TO grandson .
CONCATENATE child-ccount '1' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'g' count INTO grandson-gkey.
CONDENSE grandson-gkey NO-GAPS.
grandson-gtext = '出口成品→报关'.
grandson-gname = '1'.
grandson-gcount = count .
APPEND grandson.
CLEAR grandson.
MOVE-CORRESPONDING child TO grandson .
CONCATENATE child-ccount '2' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'g' count INTO grandson-gkey.
CONDENSE grandson-gkey NO-GAPS.
grandson-gtext = '进口料件→报关'.
grandson-gname = '1'.
grandson-gcount = count .
APPEND grandson.
CLEAR grandson.
MOVE-CORRESPONDING child TO grandson .
CONCATENATE child-ccount '3' INTO count.
CONDENSE count NO-GAPS.
CONCATENATE 'g' count INTO grandson-gkey.
CONDENSE grandson-gkey NO-GAPS.
grandson-gtext = '进料结转数→报关'.
grandson-gname = '1'.
grandson-gcount = count .
APPEND grandson.
CLEAR grandson.
ENDIF.
ENDLOOP.
ENDFORM. "set_grandson_info
*&---------------------------------------------------------------------*
*& Form set_item_info
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_item_info."曾孙处理
CLEAR nitem[].
DATA : tnum TYPE i ,
tcount TYPE string.
SELECT * FROM zsd19chuk INTO CORRESPONDING FIELDS OF TABLE i_chuk FOR ALL ENTRIES IN grandson WHERE hname = grandson-rhname.
* loop at grandson.
* select * from zsd19chuk into zsd19chuk where hname = grandson-rhname.
* if sy-subrc = 0 .
* move-corresponding zsd19chuk to i_chuk.
* append i_chuk.
* clear i_chuk.
* endif.
* endselect.
*
* clear zsd19chuk.
* endloop.
LOOP AT grandson.
IF grandson-gtext = '出口成品→维护' AND grandson-ctext = '商品信息'.
LOOP AT i_chuk WHERE hname = grandson-rhname.
tnum = tnum + 1 .
tcount = tnum.
MOVE-CORRESPONDING grandson TO nitem.
CONCATENATE grandson-gcount tcount INTO count.
CONCATENATE 'n' count INTO nitem-nkey.
CONDENSE nitem-nkey NO-GAPS.
nitem-ntext = i_chuk-extwo.
nitem-nname = '1'.
nitem-ncount = count.
APPEND nitem.
CLEAR nitem.
ENDLOOP.
tnum = 0 .
ENDIF.
ENDLOOP.
ENDFORM. "set_item_info
*&---------------------------------------------------------------------*
*& Form build_node_and_item_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM build_node_and_item_table
USING
node_table TYPE treev_ntab
item_table TYPE item_table_type.
DATA: node TYPE treev_node,
item TYPE mtreeitm.
PERFORM set_root_info.
PERFORM set_child_info.
PERFORM set_grandson_info.
PERFORM set_item_info.
LOOP AT root.
node-node_key = root-rkey.
CLEAR node-relatkey.
CLEAR node-relatship.
node-hidden = ' '.
node-disabled = ' '.
node-isfolder = 'X'.
CLEAR node-n_image.
CLEAR node-exp_image.
CLEAR node-expander.
APPEND node TO node_table.
CLEAR node.
CLEAR item.
item-node_key = root-rkey.
item-item_name = '1'. " Item with name '1'
item-class = cl_gui_list_tree=>item_class_text. " Text Item
" the with of the item is adjusted to its content (text)
item-alignment = cl_gui_list_tree=>align_auto.
" use proportional font for the item
item-font = cl_gui_list_tree=>item_font_prop.
item-text = root-rtext. "#EC NOTEXT
APPEND item TO item_table.
ENDLOOP.
*----------------------------------------------------------------------------------
LOOP AT child.
CLEAR node.
node-node_key = child-ckey.
node-relatkey = child-rkey..
node-relatship = cl_gui_list_tree=>relat_last_child.
node-isfolder = 'X'.
APPEND node TO node_table.
CLEAR item.
item-node_key = child-ckey.
item-item_name = '1'.
item-class = cl_gui_list_tree=>item_class_text.
item-alignment = cl_gui_list_tree=>align_auto.
item-font = cl_gui_list_tree=>item_font_prop.
item-text = child-ctext. "#EC NOTEXT
APPEND item TO item_table.
ENDLOOP.
*----------------------------------------------------------------------------------
LOOP AT grandson.
CLEAR node.
node-node_key = grandson-gkey.
node-relatkey = grandson-ckey.
node-relatship = cl_gui_list_tree=>relat_last_child.
node-isfolder = 'X'.
APPEND node TO node_table.
CLEAR item.
item-node_key = grandson-gkey.
item-item_name = '1'.
item-class = cl_gui_list_tree=>item_class_text.
item-alignment = cl_gui_list_tree=>align_auto.
item-font = cl_gui_list_tree=>item_font_prop.
item-text = grandson-gtext. "#EC NOTEXT
APPEND item TO item_table.
ENDLOOP.
LOOP AT nitem.
CLEAR node.
node-node_key = nitem-nkey. "#ec notext
node-relatkey = nitem-gkey.
node-relatship = cl_gui_list_tree=>relat_last_child.
APPEND node TO node_table.
CLEAR item.
item-node_key = nitem-nkey.
item-item_name = '1'.
item-class = cl_gui_list_tree=>item_class_text.
item-length = 15.
item-usebgcolor = 'x'.
item-text = nitem-ntext.
APPEND item TO item_table.
ENDLOOP.
*----------------------------------------------------------------------------------
* CLEAR NODE.
* NODE-NODE_KEY = 'New2'. "#EC NOTEXT
* NODE-RELATKEY = 'Child1'.
* NODE-RELATSHIP = CL_GUI_LIST_TREE=>RELAT_LAST_CHILD.
* APPEND NODE TO NODE_TABLE.
*
* CLEAR ITEM.
* ITEM-NODE_KEY = 'New2'.
* ITEM-ITEM_NAME = '2'.
* ITEM-CLASS = CL_GUI_LIST_TREE=>ITEM_CLASS_TEXT.
* ITEM-LENGTH = 4.
* ITEM-USEBGCOLOR = 'X'.
* ITEM-TEXT = '0200'.
* APPEND ITEM TO ITEM_TABLE.
ENDFORM. " build_node_and_item_table