Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

Dynamic ALV Tree from 1 to 5 levels - Only changing perform parameters

$
0
0

Hi All,

Here is my code to generate ALV tree with 5 hierarchy dynamically (or less according with your needs and with no change in the code)  and changing just a perform parameters to generate it. I don’t know if it have a easier and better way to do but this one is working

 

If you know another way please share

 

So follow the code:

 

Declarations:

  • My output table and work area are called, respectively: it_output and wa_output.
  • My fieldcat declaration fieldcat  TYPE lvc_t_fcat,

 

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.

 

 

Logic:

  • Hierarchic Field parameter must be called EXACTLY as the field name declarated in your output table. 
  • Ref tab and Ref Field will be used to bring the field information (data element). <reffieldX> will be used to compare the last information and the actual one and create a new node or keep the informationa agrrouped. It’s really important to have it done correctly! You can’t compare bananas to apples, right?!
  • If you want just 3 levels, clean the #4 and #5 in field_symbol. (Keep the '' though) Simply like that.

 

*                                                           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   text
*----------------------------------------------------------------------*
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 dd04d-rollname.

IF field1 IS NOT INITIAL.

ASSIGN COMPONENT field1 OF STRUCTURE wa_output TO <field1>.
CHECK sy-subrc = 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 sy-subrc = 0.

PERFORM f_fieldinfo USING table2 param2
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 <reffield2>.
ENDIF.

IF field3 IS NOT INITIAL.

ASSIGN COMPONENT field3 OF STRUCTURE wa_output TO <field3>.
CHECK sy-subrc = 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 sy-subrc = 0.

PERFORM f_fieldinfo USING table4 param4
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 <reffield4>.
ENDIF.

IF param5 IS NOT INITIAL.
ASSIGN COMPONENT field5 OF STRUCTURE wa_output TO <field5>.
CHECK sy-subrc = 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
param
CHANGING data_element.


DATA: BEGIN OF dfies OCCURS 100.
INCLUDE STRUCTURE dfies.
DATA: END OF dfies.


DATA: tablenm  TYPE ddobjname,
fieldnm 
TYPE dfies-fieldname.

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         
= sy-langu
TABLES
dfies_tab     
= dfies
EXCEPTIONS
not_found     
= 1
internal_error
= 2
OTHERS         = 3.
IF sy-subrc = 0.
READ TABLE dfies INDEX 1.
data_element
= dfies-rollname.
ENDIF.
ENDIF.

ENDFORM.                    " F_FIELDINFO


Creating hierarchies

  • Basically it will change according to what you used as parameter in perform f_field_symbol.
  • Each node have your own perform because if you want to fill the 2nd node with something different to the standard counts (sum, avg, max, min, ... – flagging H_FTYPE on fieldcat and using call method tree1->update_calculations.)  you need to use the same work are type used in your detailed line and organize it to show up what you want to.

 

*&---------------------------------------------------------------------*
*&      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 vakevb-kotabnr,
l_knuma    
TYPE vakevb-knuma,
l_desc
(100) TYPE c,
l_kschl    
TYPE vakevb-kschl,
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_layout
-t_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_node
-isfolder = '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_layout
-t_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_layout
-t_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_level
-knuma   = wa_output-knuma.

CONCATENATE wa_output-datab+6(2)
wa_output
-datab+4(2)
wa_output
-datab(4)
INTO wa_level-zzbrandd
SEPARATED BY '.'.

CONCATENATE wa_output-datbi+6(2)
wa_output
-datbi+4(2)
wa_output
-datbi(4)
INTO wa_level-kunnr
SEPARATED BY '.'.

wa_level
-matnr   = wa_output-waers.
wa_level
-kondm   = wa_output-agnotes.
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_layout
-t_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_level
-zzbrandd = wa_output-combin.
wa_level
-level   = hierarchy.

wa_level
-knuma = wa_output-knuma.

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_layout
-t_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_level
-zzbrandd = wa_output-combin.
wa_level
-level   = hierarchy.

wa_level
-knuma = wa_output-knuma.

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

*&---------------------------------------------------------------------*
*&      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_layout
-fieldname = tree1->c_hierarchy_column_name.
APPEND ls_item_layout TO lt_item_layout.

l_node_text
= wa_output-bonem.

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_output
-level = 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

 

 

 

I think that is everything here. Let me know if I missed something or someone need help to understand it.

Hope it helps.

Regards,

Andréa


Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>