Skip to Content

Olá,


Aqui esta meu código para criar um ALV tree com até 5 hierarquias mudando apenas os parametros de um perform pra isso. Pode ser que exista outra forma mais fácil e eu esteja reinventando a roda mas esse ta funcionando. Se alguém souber outra forma, por favor, compartilhe 😉


Declarações:

  • Minha tabela e work area de exibicao chamam, respectivamente: it_output and wa_output. (voce vai encontra-las algumas vezes por ai…)

DATA: tree1  TYPE REF TO cl_gui_alv_tree.DATA: v_icon1            TYPE lvc_s_layi,
    v_icon2 
TYPE lvc_s_layi,
    v_icon3 
TYPE lvc_s_layi,
    v_icon4
TYPE lvc_s_layi,
    v_icon5 
TYPE lvc_s_layi.
FIELD-SYMBOLS: <field1>    TYPE any,
              <field2>   
TYPE any,
              <field3>   
TYPE any,
              <field4>   
TYPE any,
              <field5>   
TYPE any,
              <reffield1>
TYPE any,
              <reffield2>
TYPE any,
              <reffield3>
TYPE any,
              <reffield4>
TYPE any,
              <reffield5>
TYPE any.

Logica:

  • Os campos de hierarquia que serão declarados em f_field_symbo devem ser EXATAMENTE iguais ao da tabela it_output.
  • Os parametros Ref tab e Ref Field são usados para criar os field symbols de comparação. Ou seja, eles devem estar corretos pq você sabe o que acontece comparar Char com Numc, né?! DUMP
  • Pra usar um ALV com 3 nivels é só deixar os parametros da linha 4 e 5 vazios. (”) Só isso!

*                                                          Hierarchic Field / Ref Tab /  Ref Field

PERFORM f_field_symbol USING: ‘TYPED’    ‘T6B1T’      ‘VTEXT’ “1
                             
‘DESC’      ‘TB038B’    ‘TEXT’  “2
                             
‘KNUMA’    ‘KONA’      ‘KNUMA’ “3
                             
‘KSCHL’    ‘VAKEVB’  ‘KSCHL’ “4
                             
‘KOTABNR’ ‘VAKEVB’  ‘KOTABNR’. “5

*&———————————————————————*

*&    Form  F_FIELD_SYMBOL

*&———————————————————————*

*      text

*———————————————————————-*

*      –>P_0299  text

*      –>P_0300  text

*      –>P_0301  tex

*———————————————————————-*

FORM f_field_symbol  USING field1 table1 param1                           

                           field2 table2 param2
                           field3 table3 param3
                           field4 table4 param4
                           field5 table5 param5
.


DATA r_elemdescr  TYPE REF TO cl_abap_elemdescr.

DATA r_field      TYPE REF TO data.

DATA: data_element TYPE dd04drollname.


IF field1 IS NOT INITIAL.

ASSIGN COMPONENT field1 OF STRUCTURE wa_output TO <field1>.

CHECK sysubrc = 0.


PERFORM f_fieldinfo USING table1 param1 CHANGING data_element.

r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( data_element ).

CREATE DATA r_field TYPE HANDLE r_elemdescr.

ASSIGN r_field->* TO <reffield1>.

ENDIF.


IF field2 IS NOT INITIAL.

ASSIGN COMPONENT field2 OF STRUCTURE wa_output TO <field2>.

CHECK sysubrc = 0.


PERFORM f_fieldinfo USING table2 param2CHANGING data_element.

r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( data_element ).

CREATE DATA r_field TYPE HANDLE r_elemdescr.

ASSIGN r_field->* TO <reffield2>.

ENDIF.


IF field3 IS NOT INITIAL.

ASSIGN COMPONENT field3 OF STRUCTURE wa_output TO <field3>.

CHECK sysubrc = 0.


PERFORM f_fieldinfo USING table3 param3 CHANGING data_element.

r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( data_element ).

CREATE DATA r_field TYPE HANDLE r_elemdescr.

ASSIGN r_field->* TO <reffield3>.

ENDIF.


IF field4 IS NOT INITIAL.

ASSIGN COMPONENT field4 OF STRUCTURE wa_output TO <field4>.

CHECK sysubrc = 0.


PERFORM f_fieldinfo USING table4 param4CHANGING data_element.

r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( data_element ).


CREATE DATA r_field TYPE HANDLE r_elemdescr.

ASSIGN r_field->* TO <reffield4>.

ENDIF.


IF param5 IS NOT INITIAL.

ASSIGN COMPONENT field5 OF STRUCTURE wa_output TO <field5>.

CHECK sysubrc = 0.


PERFORM f_fieldinfo USING table5 param5 CHANGING data_element.

r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( data_element ).

CREATE DATA r_field TYPE HANDLE r_elemdescr.

ASSIGN r_field->* TO <reffield5>.

ENDIF.


PERFORM build_sort_table USING field1 field2 field3 field4 field5.


ENDFORM.                    ” F_FIELD_SYMBOL


*&———————————————————————*

*&      Form  F_FIELDINFO

*&———————————————————————*

*      text

*———————————————————————-*

*      –>P_TABLE1  text

*      –>P_PARAM1  text

*      <–P_DATA_ELEMENT  text

*———————————————————————-*

FORM f_fieldinfo  USING    table paramCHANGING data_element.


DATA: BEGIN OF dfies OCCURS 100.

INCLUDE STRUCTURE dfies.

DATA: END OF dfies.


DATA: tablenm  TYPE ddobjname,
         fieldnm 
TYPE dfiesfieldname.


MOVE table TO tablenm.

MOVE param TO fieldnm.



***  Fname Description

IF NOT fieldnm IS INITIAL.

CALL FUNCTION ‘DDIF_FIELDINFO_GET’EXPORTING
tabname       
= tablenm
fieldname     
= fieldnm
langu         
= sylangu

TABLES
dfies_tab     
= dfies

EXCEPTIONS
not_found     
= 1
internal_error
= 2

OTHERS        = 3.

IF sysubrc = 0.

READ TABLE dfies INDEX 1.

data_element = dfiesrollname.

ENDIF.

ENDIF.

ENDFORM.                    ” F_FIELDINFO


Creating hierarchies

  • Basicamente essa lógica vai usar as declarações que você fez lá no perform f_field_symbol.
  • Cada nó tem um perform diferente pq se vc quiser usar uma informação que não seja os contadores basicos fornecidos pelo fieldcat (soma, média, max, min ) você vai ter que combinar os valores da linha de detalhe com as linhas de hierarquia.

*&———————————————————————*
*&      Form  CREATE_HIERARCHY
*&———————————————————————*
*      text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM create_hierarchy .

* add data to tree
DATA: l_last_key  TYPE lvc_nkey,
    l_kotabnr 
TYPE vakevbkotabnr,
    l_knuma   
TYPE vakevbknuma,
    l_desc
(100) TYPE c,
    l_kschl   
TYPE vakevbkschl,
    l_add     
TYPE c.

DATA: l_param_key  TYPE lvc_nkey,
    l_param2_key
TYPE lvc_nkey,
    l_param3_key
TYPE lvc_nkey,
    l_param4_key
TYPE lvc_nkey,
    l_param5_key
TYPE lvc_nkey.

LOOP AT it_output INTO wa_output.

**** LEVEL 1
IF <field1> IS ASSIGNED.
IF <field2> IS ASSIGNED. “Level 2 is empty
IF <field1> IS NOT INITIAL.
IF <reffield1> NE <field1>.
PERFORM level1 USING
v_icon1
fieldname
CHANGING l_param_key.

IF <field2> IS ASSIGNED.
CLEAR: <reffield2>.
ENDIF.

IF <field3> IS ASSIGNED.
CLEAR: <reffield3>.
ENDIF.

IF <field4> IS ASSIGNED.
CLEAR: <reffield4>.
ENDIF.

APPEND l_param_key TO it_expand_nodes.

ENDIF.
ENDIF.
ELSE.
“If next level is empty so finish the hierarchy
PERFORM add_complete_line USING 2
CHANGING l_last_key.
l_add
= ‘X’.
ENDIF.
ENDIF.

**** LEVEL 2
IF <field2> IS ASSIGNED.
IF <field3> IS ASSIGNED. “Level 3 is empty
IF <field2> IS NOT INITIAL.
IF <reffield2> NE <field2>.
PERFORM level2 USING l_param_key
2
v_icon2
fieldname
CHANGING l_param2_key.

IF <field3> IS ASSIGNED.
CLEAR: <reffield3>.
ENDIF.

IF <field4> IS ASSIGNED.
CLEAR: <reffield4>.
ENDIF.

APPEND l_param2_key TO it_expand_nodes.

ENDIF.
ENDIF.
ELSE.
“If next level is empty so finish the hierarchy
PERFORM add_complete_line USING L_PARAM_KEY 3
CHANGING l_last_key.
l_add
= ‘X’.
ENDIF.
ENDIF.

*** LEVEL 3
IF <field3> IS ASSIGNED.
IF <field4> IS ASSIGNED. “Level 4 is empty
IF <field3> IS NOT INITIAL.
IF <reffield3> NE <field3>.
PERFORM level3 USING l_param2_key
3
v_icon3
fieldname
CHANGING l_param3_key .

IF <field4> IS ASSIGNED.
CLEAR: <reffield4>.
ENDIF.

APPEND  l_param3_key TO it_expand_nodes.

ENDIF.
ENDIF.
ELSE.
“If next level is empty so finish the hierarchy
PERFORM add_complete_line USING l_param2_key 4
CHANGING l_last_key.
l_add
= ‘X’.
ENDIF.
ENDIF.

*** LEVEL 4
IF <field4> IS ASSIGNED.
IF <field5> IS ASSIGNED. “Level 4 is empty
IF <field4> IS NOT INITIAL.
IF <reffield4> NE <field4>.
PERFORM level4 USING l_param3_key
3
v_icon3
fieldname
CHANGING l_param4_key .

IF <field5> IS ASSIGNED.
CLEAR: <reffield5>.
ENDIF.

APPEND  l_param4_key TO it_expand_nodes.

ENDIF.
ENDIF.
ELSE.
“If next level is empty so finish the hierarchy
PERFORM add_complete_line USING l_param3_key 4
CHANGING l_last_key.
l_add
= ‘X’.
ENDIF.
ENDIF.

*** LEVEL 5
IF <field5> IS ASSIGNED.
IF <field5> IS NOT INITIAL.
IF <reffield5> NE <field5>.
PERFORM level5 USING l_param4_key
4
v_icon5
fieldname
CHANGING l_param5_key .

APPEND  l_param4_key TO it_expand_nodes.

PERFORM add_complete_line USING l_param5_key 6
CHANGING l_last_key.
l_add
= ‘X’.

ENDIF.
ENDIF.
ENDIF.

IF l_add IS INITIAL.
PERFORM add_complete_line USING l_param5_key 5
CHANGING l_last_key.
ENDIF.

IF <field1> IS ASSIGNED.
<reffield1>
= <field1>.
ENDIF.

IF <field2> IS ASSIGNED.
<reffield2>
= <field2>.
ENDIF.

IF <field3> IS ASSIGNED.
<reffield3>
= <field3>.
ENDIF.

IF <field4> IS ASSIGNED.
<reffield4>
= <field4>.
ENDIF.

IF <field5> IS ASSIGNED.
<reffield5>
= <field5>.
ENDIF.

CLEAR: l_add.

ENDLOOP.

*&———————————————————————*
*&      Form  LEVEL1
*&———————————————————————*
*      text
*———————————————————————-*
*      –>P_WA_DATA  text
*      –>P_L_PARAM2_KEY  text
*      –>P_3      text
*      –>P_1748  text
*      <–P_L_PARAM3_KEY  text
*———————————————————————-*
FORM level1 USING  p_relat_key TYPE lvc_nkey
                  p_icon
                      CHANGING p_node_key.

DATA: l_node_text TYPE lvc_value,
    relat     
TYPE int4,
    wa_refe   
TYPE tab_type,
    wa_level 
TYPE ty_output.

* set item-layout
DATA: lt_item_layout TYPE lvc_t_layi,
    ls_item_layout
TYPE lvc_s_layi.

DATA: ls_node TYPE lvc_s_layn.
    ls_node
n_image  = space.
    ls_node
exp_image = space.

ls_item_layoutt_image = p_icon.
ls_item_layout
style  = cl_gui_column_tree=>style_intensified.
ls_item_layout
fieldname = tree1->c_hierarchy_column_name.
APPEND ls_item_layout TO lt_item_layout.

* add node
l_node_text
= <field1>.
wa_level
level  = 1.

ls_nodeisfolder = ‘X’.

CALL METHOD tree1->add_node
EXPORTING
i_relat_node_key   
= p_relat_key
i_relationship     
= cl_gui_column_tree=>relat_last_child
i_node_text         
= l_node_text
is_outtab_line     
= wa_level
is_node_layout     
= ls_node
it_item_layout     
= lt_item_layout
IMPORTING
e_new_node_key     
= p_node_key
EXCEPTIONS
relat_node_not_found
= 1
node_not_found     
= 2
OTHERS              = 3.

ENDFORM.                    ” LEVEL1


*&———————————————————————*
*&      Form  LEVEL2
*&———————————————————————*
*      text
*———————————————————————-*
*      –>P_L_PARAM1_KEY  text
*      –>P_2      text
*      –>P_1721  text
*      <–P_L_PARAM2_KEY  text
*———————————————————————-*
FORM level2  USING p_relat_key TYPE lvc_nkey
hierarchy
icon
CHANGING p_node_key TYPE lvc_nkey.

* set item-layout
DATA: lt_item_layout TYPE lvc_t_layi,
    l_node_text   
TYPE lvc_value,
    ls_item_layout
TYPE lvc_s_layi,
    relat         
TYPE int4,
    wa_level     
TYPE ty_output.

ls_item_layoutt_image = icon.
ls_item_layout
style  cl_gui_column_tree=>style_intensified.
ls_item_layout
fieldname = tree1->c_hierarchy_column_name.
APPEND ls_item_layout TO lt_item_layout.

* add node
l_node_text
= <field2>.
wa_level
level = hierarchy.

relat = cl_gui_column_tree=>relat_last_child.
CALL METHOD tree1->add_node
EXPORTING
i_relat_node_key
= p_relat_key
i_relationship 
= relat
i_node_text     
= l_node_text
is_outtab_line 
= wa_level
it_item_layout 
= lt_item_layout
IMPORTING
e_new_node_key 
= p_node_key.

ENDFORM.                    “LEVEL2

*&———————————————————————*
*&      Form  LEVEL3
*&———————————————————————*
*      text
*———————————————————————-*
*      –>P_L_PARAM1_KEY  text
*      –>P_2      text
*      –>P_1721  text
*      <–P_L_PARAM2_KEY  text
*———————————————————————-*
FORM level3  USING p_relat_key TYPE lvc_nkey
hierarchy
icon
CHANGING p_node_key TYPE lvc_nkey.

* set item-layout
DATA: lt_item_layout TYPE lvc_t_layi,
    l_node_text   
TYPE lvc_value,
    ls_item_layout
TYPE lvc_s_layi,
    relat         
TYPE int4,
    wa_level     
TYPE ty_output.

ls_item_layoutt_image = icon.
ls_item_layout
style  cl_gui_column_tree=>style_intensified.
ls_item_layout
fieldname = tree1->c_hierarchy_column_name.
APPEND ls_item_layout TO lt_item_layout.

* add node
l_node_text
= <field3>.

wa_levelknuma  = wa_outputknuma.

CONCATENATE wa_outputdatab+6(2)
wa_output
datab+4(2)
wa_output
datab(4)
INTO wa_levelzzbrandd
SEPARATED BY ‘.’.

CONCATENATE wa_outputdatbi+6(2)
wa_output
datbi+4(2)
wa_output
datbi(4)
INTO wa_levelkunnr
SEPARATED BY ‘.’.

wa_levelmatnr  = wa_outputwaers.
wa_level
kondm  = wa_outputagnotes.
wa_level
level  = hierarchy.

relat = cl_gui_column_tree=>relat_last_child.
CALL METHOD tree1->add_node
EXPORTING
i_relat_node_key
= p_relat_key
i_relationship 
= relat
i_node_text     
= l_node_text
is_outtab_line 
= wa_level
it_item_layout 
= lt_item_layout
IMPORTING
e_new_node_key 
= p_node_key.

ENDFORM.                    ” LEVEL3

*&———————————————————————*
*&      Form  LEVEL4
*&———————————————————————*
*      text
*———————————————————————-*
*      –>P_L_PARAM1_KEY  text
*      –>P_2      text
*      –>P_1721  text
*      <–P_L_PARAM2_KEY  text
*———————————————————————-*
FORM level4  USING p_relat_key TYPE lvc_nkey
hierarchy
icon
CHANGING p_node_key TYPE lvc_nkey.

* set item-layout
DATA: lt_item_layout TYPE lvc_t_layi,
    l_node_text   
TYPE lvc_value,
    ls_item_layout
TYPE lvc_s_layi,
    relat         
TYPE int4,
    wa_level     
TYPE ty_output.

ls_item_layoutt_image = icon.
ls_item_layout
style  cl_gui_column_tree=>style_intensified.
ls_item_layout
fieldname = tree1->c_hierarchy_column_name.
APPEND ls_item_layout TO lt_item_layout.

* add node

l_node_text = <field4>.

wa_levelzzbrandd = wa_outputcombin.
wa_level
level  = hierarchy.

wa_levelknuma = wa_outputknuma.

relat = cl_gui_column_tree=>relat_last_child.
CALL METHOD tree1->add_node
EXPORTING
i_relat_node_key
= p_relat_key
i_relationship 
= relat
i_node_text     
= l_node_text
is_outtab_line 
= wa_level
it_item_layout 
= lt_item_layout
IMPORTING
e_new_node_key 
= p_node_key.

ENDFORM.                    “LEVEL4

*&———————————————————————*
*&      Form  LEVEL5
*&———————————————————————*
*      text
*———————————————————————-*
*      –>P_L_PARAM1_KEY  text
*      –>P_2      text
*      –>P_1721  text
*      <–P_L_PARAM2_KEY  text
*———————————————————————-*
FORM level5  USING p_relat_key TYPE lvc_nkey
hierarchy
icon
CHANGING p_node_key TYPE lvc_nkey.

* set item-layout
DATA: lt_item_layout TYPE lvc_t_layi,
    l_node_text   
TYPE lvc_value,
    ls_item_layout
TYPE lvc_s_layi,
    relat         
TYPE int4,
    wa_level     
TYPE ty_output.

ls_item_layoutt_image = icon.
ls_item_layout
style  cl_gui_column_tree=>style_intensified.
ls_item_layout
fieldname = tree1->c_hierarchy_column_name.
APPEND ls_item_layout TO lt_item_layout.

* add node

l_node_text = <field5>.

wa_levelzzbrandd = wa_outputcombin.
wa_level
level  = hierarchy.

wa_levelknuma = wa_outputknuma.

relat = cl_gui_column_tree=>relat_last_child.
CALL METHOD tree1->add_node
EXPORTING
i_relat_node_key
= p_relat_key
i_relationship 
= relat
i_node_text     
= l_node_text
is_outtab_line 
= wa_level
it_item_layout 
= lt_item_layout
IMPORTING
e_new_node_key 
= p_node_key.

ENDFORM.                    “LEVEL5

Não consegui postar tudo aqui.. tem mais um perform nos comentários …

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Andrea Molina Post author


    *&———————————————————————*
    *&      Form  add_cmplete_line
    *&———————————————————————*
    *      add hierarchy-level 3 to tree
    *———————————————————————-*
    *      –>P_LS_SFLIGHT  sflight
    *      –>P_RELEATKEY  relatkey
    *    <–>p_node_key    new node-key
    *———————————————————————-*
    FORM add_complete_line USING  p_relat_key TYPE lvc_nkey
    hierarchy
    CHANGING  p_node_key TYPE lvc_nkey.

    DATA: l_node_text TYPE lvc_value.

    * set item-layout
    DATA: lt_item_layout TYPE lvc_t_layi,
        ls_item_layout
    TYPE lvc_s_layi.

    ls_item_layoutfieldname = tree1->c_hierarchy_column_name.
    APPEND ls_item_layout TO lt_item_layout.

    l_node_text = wa_outputbonem.

    IF l_node_text IS INITIAL.
    MESSAGE s021(zgeral) DISPLAY LIKE ‘E’.
    LEAVE LIST-PROCESSING.
    ENDIF.

    DATA: ls_node TYPE lvc_s_layn.
    ls_node
    n_image  = space.
    ls_node
    exp_image = space.

    wa_outputlevel = hierarchy.

    CALL METHOD tree1->add_node
    EXPORTING
    i_relat_node_key
    = p_relat_key
    i_relationship 
    = cl_gui_column_tree=>relat_last_child
    is_outtab_line 
    = wa_output
    i_node_text     
    = l_node_text
    is_node_layout 
    = ls_node
    it_item_layout 
    = lt_item_layout
    IMPORTING
    e_new_node_key 
    = p_node_key.
    ENDFORM.                              ” add_complete_line


    É isso.. Acho que esta tudo aqui.. Se faltou algum perform ou alguma coisa me avisem… E se alguém estiver com dúvidas tbm..


    Espero que isso ajude

    Andréa

    (0) 

Leave a Reply