cancel
Showing results for 
Search instead for 
Did you mean: 

ALV Tree Report

Former Member
0 Kudos

Dear Experts,

I want to build a tree report for Batch reconciliation for below table.

Input Batch          Output Batch

1R0001                    1R0001A                  

1R0001                    1R0001B

1R0001                    1R0001C

1R0001A                  1R0001A1

1R0001A                  1R0001A2

1R0001B                  1R0001B1

1R0001C                  1R0001C1

1R0001C                  1R0001C2

1R0001C                  1R0001C3

1R0001C3                1R0001C3A

I want to build a tree report like below.

Tree side                                                Batch Information Column

Tree                                                       Column1          Column2          Column3          Column(n).

1R0001

          1R0001A                  

                        1R0001A1

                        1R0001A2

          1R0001B

                        1R0001B1

          1R0001C

                        1R0001C1

                        1R0001C2

                        1R0001C3

                                        1R0001C3A


How to code for the above tree and which function module or class useful for me.

Main problem in this logic is my tree levels are dynamic it can be 2 level or 20 level.

I request to Moderator : Pl. don't close this discussion, because it is not about a simple tree report, it about a dynamic levels tree report.


Accepted Solutions (0)

Answers (1)

Answers (1)

kesavadas_thekkillath
Active Contributor
0 Kudos

Moderator Message: Unmarked as question as there was no enough research.

In SE38 search for BCALV_TREE_DEMO. From next time try to do some research yourself before asking the question.

Former Member
0 Kudos

Dear,

I have research about that that but all examples about fixed levels, and as you tell me about BCALV_TREE_DEMO program, that is not useful for me.

please understand my problem and read again about my problem.

I want to use CL_GUI_ALV_GRID for the same.

how can I code for above mentioned Internal table. my levels are dynamic.

In our company too many work center and a single split into various part at different stages and user use MB1B(Transfer posting between stages). I Populate the table as I mention in my question.

Pl. provide logic for that if possible.

kesavadas_thekkillath
Active Contributor
0 Kudos

You will not be able to do it with CL_GUI_ALV_GRID.

In BCALV_TREE_DEMO, how do you say it is fixed. No its not fixed. You have to create node and build the hierarchy levels based on the data you have. Refer the subroutine create_hierarchy and the internal subroutines add_carrid_line,add_connid_line,add_complete_line which creates nodes in a loop for all the values( nothing is fixed here). Similarly , you have to code your work center split.

If you feel this is different, then differentiate it with the term "dynamic" , as you mentioned.

Former Member
0 Kudos

data : begin of it1 occurs 0,

               icharg type mch1-charg,

               ocharg type mch1-charg,

               imenge type mseg-menge,

               omenge type mseg-menge,

               imatnr type mch1-matnr,

               omatnr type mch1-matnr,

          end of it1.

define add_record.

it1-icharg = &1.

it1-ocharg = &1.

append it1. clear it1.

end-of-definition.

start-of-selection.

add_record :

'1R0001'                    '1R0001A' ,                

'1R0001'                    '1R0001B',

'1R0001'                    '1R0001C',

'1R0001A'                  '1R0001A1',

'1R0001A'                  '1R0001A2',

'1R0001B'                  '1R0001B1',

'1R0001C'                  '1R0001C1',

'1R0001C'                  '1R0001C2',

'1R0001C'                  '1R0001C3',

'1R0001C3'                '1R0001C3A'.

Internal table IT1 with contains:.

Input Batch          Output Batch

1R0001                    1R0001A                 

1R0001                    1R0001B

1R0001                    1R0001C

1R0001A                  1R0001A1

1R0001A                  1R0001A2

1R0001B                  1R0001B1

1R0001C                  1R0001C1

1R0001C                  1R0001C2

1R0001C                  1R0001C3

1R0001C3                1R0001C3A

It it typical because for the above table I want below output:

OUTPUT:

Tree side                                              Batch Information Column

Tree                                                       IMENGE          OMENGE          IMATNR          OMATNR.

1R0001

          1R0001A                 

                        1R0001A1

                        1R0001A2

          1R0001B

                        1R0001B1

          1R0001C

                        1R0001C1

                        1R0001C2

                        1R0001C3

                                        1R0001C3A

Try to code for above requirement, and you will understand my problems.

I can use any FM or Class for above mentioned output.

kesavadas_thekkillath
Active Contributor
0 Kudos

Okay, I have written the logic for you. I kindly advice you think well while coding a logic instead of directly coming here and asking it. As a programmer "We have to break our head a bit". In the below program the ALV formatting must be done your self, the core recursive logic of parent and child is available.

REPORT zkm_tree.

TYPES:BEGIN OF ty,

       icharg TYPE mch1-charg,

       ocharg TYPE mch1-charg,

       imenge TYPE mseg-menge,

       omenge TYPE mseg-menge,

       imatnr TYPE mch1-matnr,

       omatnr TYPE mch1-matnr,

       END OF ty,

tb_d  TYPE TABLE OF ty,

   BEGIN OF ty1,

     child    TYPE string,

     parent   TYPE string,

     key      TYPE salv_de_node_key,

   END OF ty1,

   tb_h  TYPE TABLE OF ty1.

DATA:

   it_data  TYPE tb_d,

   it_hier  TYPE tb_h,

   wa_data  TYPE ty,

   wa_hier TYPE ty1.

DEFINE add_record.

   wa_data-icharg = &1.

   wa_data-ocharg = &2.

   append wa_data to it_data.

END-OF-DEFINITION.

START-OF-SELECTION.

   add_record :'1R0001' '1R0001A' ,'1R0001' '1R0001B',

               '1R0001' '1R0001C', '1R0001A' '1R0001A1',

               '1R0001A' '1R0001A2','1R0001B' '1R0001B1',

               '1R0001C' '1R0001C1','1R0001C' '1R0001C2',

               '1R0001C' '1R0001C3','1R0001C3' '1R0001C3A'.

   SORT it_data BY icharg.

   READ TABLE it_data INTO wa_data INDEX 1.

   CHECK sy-subrc = 0.

   PERFORM scan USING wa_data-icharg '' CHANGING it_hier.

   PERFORM display.

FORM scan USING    child   TYPE mch1-charg

                         parent TYPE mch1-charg

                CHANGING pt_hier   TYPE tb_h.

   DATA:

      wa_hier  TYPE ty1,

      lv_child TYPE mch1-charg,

      lv_parent TYPE mch1-charg.

   wa_hier-child   = child.

   wa_hier-parent = parent.

   APPEND wa_hier TO pt_hier.

   CLEAR wa_hier.

   lv_parent = child.

   LOOP AT it_data INTO wa_data WHERE icharg = lv_parent.

     lv_child = wa_data-ocharg.

     PERFORM scan USING lv_child lv_parent CHANGING pt_hier.

   ENDLOOP.

ENDFORM.                  

FORM display.

   DATA:

     lt_init          TYPE tb_d,

     wa_data          TYPE ty,

     wa_hier          TYPE ty1,

     lv_key           TYPE salv_de_node_key,

     lf_tree          TYPE REF TO cl_salv_tree,

     lf_nodes         TYPE REF TO cl_salv_nodes,

     lf_node          TYPE REF TO cl_salv_node,

     lf_columns       TYPE REF TO cl_salv_columns,

     lf_column        TYPE REF TO cl_salv_column,

     lf_setting       TYPE REF TO cl_salv_tree_settings,

     lv_text          TYPE lvc_value.

   FIELD-SYMBOLS:<fs> TYPE ty1.

   TRY.

       CALL METHOD cl_salv_tree=>factory

         IMPORTING

           r_salv_tree = lf_tree

         CHANGING

           t_table     = lt_init.

       lf_nodes = lf_tree->get_nodes( ).

       LOOP AT it_hier ASSIGNING <fs>.

         IF <fs>-parent = ''.

           lf_node = lf_nodes->add_node( related_node = ''

                     relationship = if_salv_c_node_relation=>parent ).

         ELSE.

           CLEAR wa_hier.

           READ TABLE it_hier INTO wa_hier

             WITH KEY child = <fs>-parent.

           lf_node = lf_nodes->add_node(

            related_node = wa_hier-key

            relationship = if_salv_c_node_relation=>first_child ).

         ENDIF.

         <fs>-key = lf_node->get_key( ).

         CLEAR wa_data.

         IF <fs>-parent = ''.

           READ TABLE it_data INTO wa_data

            WITH KEY icharg = <fs>-child.

           IF sy-subrc = 0.

             lv_text = <fs>-child.

             lf_node->set_text( lv_text ).

           ENDIF.

         ENDIF.

         READ TABLE it_data INTO wa_data

           WITH KEY ocharg = <fs>-child.

         IF sy-subrc = 0.

           lf_node->set_data_row( wa_data ).

           lv_text = <fs>-child.

           lf_node->set_text( lv_text ).

         ENDIF.

       ENDLOOP.

       lf_columns = lf_tree->get_columns( ).

       lf_columns->set_optimize( abap_true ).

       lf_column = lf_columns->get_column( 'ICHARG' ).

       lf_column->set_visible( abap_false ).

       lf_column = lf_columns->get_column( 'OCHARG' ).

       lf_column->set_visible( abap_false ).

       lf_setting = lf_tree->get_tree_settings( ).

       lf_setting->set_hierarchy_header( 'Split' ).

       lf_nodes->expand_all( ).

       lf_tree->display( ).

     CATCH cx_salv_error.

   ENDTRY.

ENDFORM.                  

Former Member
0 Kudos

Dear Kesavadas,

I appreciate your job which you have done for me.

but you use CL_SALV_TREE and i was using CL_GUI_ALV_GRID.

CL_GUI_ALV_GRID provide many functions to user and CL_SALV_TREE has limited functions.

like I want to expand tree at single level  but in this class Expend all or collapse all.

save layout etc.

But thanks for recursive logic in this class and it work fine.

kesavadas_thekkillath
Active Contributor
0 Kudos

Refer sample programs starting with SALV_DEMO_TREE*. Everything is possible in SALV tree except editing.