Skip to Content
author's profile photo Former Member
Former Member

ALV REPORT TREE

Hi ABAP Experts,

I have to add ALV Report tree functionality in a report program.

Can anybody provide me some sample code or programs related to ALV report tree creation ?

Also please mention the necessary steps required for ALV report tree creation.

Thanks,

Akash

Add a comment
10|10000 characters needed characters exceeded

Related questions

4 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Apr 10, 2008 at 06:55 AM

    hi there,,,,,,check this..

    report y13816_alv_tree_with_oops.

    §1a. Define reference variables

    data: g_alv_tree type ref to cl_gui_alv_tree,

    g_custom_container type ref to cl_gui_custom_container,

    gt_sflight type sflight occurs 0,

    g_max type i value 255.

    end-of-selection.

    call screen 100.

    &----

    -


    *& Module STATUS_0100 OUTPUT

    &----

    -


    text

    -

    -


    module status_0100 output.

    set pf-status 'MENU_BAR'.

    if g_alv_tree is initial.

    perform init_tree.

    call method cl_gui_cfw=>flush

    exceptions

    cntl_system_error = 1

    cntl_error = 2.

    if sy-subrc 0.

    call function 'POPUP_TO_INFORM'

    exporting

    titel = 'Automation Queue failure'(801)

    txt1 = 'Internal error:'(802)

    txt2 = 'A method in the automation queue'(803)

    txt3 = 'caused a failure.'(804).

    endif.

    endif.

    endmodule. " STATUS_0100 OUTPUT

    &----

    -


    *& Form init_tree

    &----

    -


    text

    -

    -


    --> p1 text

    <-- p2 text

    -

    -


    form init_tree .

    §1b. Create ALV Tree Control and corresponding Container.

    create container for alv-tree

    data: l_tree_container_name(30).

    l_tree_container_name = 'CONTAINER'.

    create object g_custom_container

    exporting

    container_name = l_tree_container_name

    exceptions

    cntl_error = 1

    cntl_system_error = 2

    create_error = 3

    lifetime_error = 4

    lifetime_dynpro_dynpro_link = 5.

    if sy-subrc <> 0.

    message x208(00) with 'ERROR'(100).

    endif.

    Creating Tree Control

    create object g_alv_tree

    exporting

    parent = g_custom_container

    node_selection_mode = cl_gui_column_tree=>node_sel_mode_single

    item_selection = 'X'

    no_html_header = 'X'

    no_toolbar = 'X'

    exceptions

    cntl_error = 1

    cntl_system_error = 2

    create_error = 3

    lifetime_error = 4

    illegal_node_selection_mode = 5

    failed = 6

    illegal_column_name = 7.

    if sy-subrc 0.

    message x208(00) with 'ERROR'. "#EC NOTEXT

    endif.

    §2. Create Hierarchy-header

    The simple ALV Tree uses the text of the fields which were used

    for sorting to define this header. When you use

    the 'normal' ALV Tree the hierarchy is build up freely

    by the programmer this is not possible, so he has to define it

    himself.

    data: l_hierarchy_header type treev_hhdr.

    perform build_hierarchy_header changing l_hierarchy_header.

    §3. Create empty Tree Control

    IMPORTANT: Table 'gt_sflight' must be empty. Do not change this table

    (even after this method call). You can change data of your table

    by calling methods of CL_GUI_ALV_TREE.

    Furthermore, the output table 'gt_outtab' must be global and can

    only be used for one ALV Tree Control.

    call method g_alv_tree->set_table_for_first_display

    exporting

    i_structure_name = 'SFLIGHT'

    is_hierarchy_header = l_hierarchy_header

    changing

    it_outtab = gt_sflight. "table must be empty !

    §4. Create hierarchy (nodes and leaves)

    perform create_hierarchy.

    §5. Send data to frontend.

    call method g_alv_tree->frontend_update.

    endform. " init_tree

    &----

    -


    *& Form build_hierarchy_header

    &----

    -


    text

    -

    -


    <--P_L_HIERARCHY_HEADER text

    -

    -


    form build_hierarchy_header changing

    p_hierarchy_header type treev_hhdr.

    p_hierarchy_header-heading = 'Month/Carrier/Date'.

    p_hierarchy_header-tooltip = 'Flights in a Month'.

    p_hierarchy_header-width = 30.

    p_hierarchy_header-width_pix = ' '.

    endform. " build_hierarchy_header

    &----

    -


    *& Form create_hierarchy

    &----

    -


    text

    -

    -


    --> p1 text

    <-- p2 text

    -

    -


    form create_hierarchy .

    data: ls_sflight type sflight,

    lt_sflight type sflight occurs 0,

    l_yyyymm(6) type c,

    l_yyyymm_last(6) type c,

    l_carrid like sflight-carrid,

    l_carrid_last like sflight-carrid,

    l_month_key type lvc_nkey,

    l_carrid_key type lvc_nkey,

    l_last_key type lvc_nkey.

    §4a. Select data

    select * from sflight into table lt_sflight up to g_max rows.

    §4b. Sort output table according to your conceived hierarchy

    We sort in this order:

    year and month (top level nodes, yyyymm of DATS)

    carrier id (next level)

    day of month (leaves, dd of DATS)

    sort lt_sflight by fldate0(6) carrid fldate6(2).

    Note: The top level nodes do not correspond to a field of the

    output table. Instead we use data of the table to invent another

    hierarchy level above the levels that can be build by sorting.

    §4c. Add data to tree

    loop at lt_sflight into ls_sflight.

    Prerequesite: The table is sorted.

    You add a node everytime the values of a sorted field changes.

    Finally, the complete line is added as a leaf below the last

    node.

    l_yyyymm = ls_sflight-fldate+0(6).

    l_carrid = ls_sflight-carrid.

    Top level nodes:

    if l_yyyymm <> l_yyyymm_last.

    l_yyyymm_last = l_yyyymm.

    *Providing no key means that the node is added on top level:

    perform add_month using l_yyyymm

    ''

    changing l_month_key.

    The month changed, thus, there is no predecessor carrier

    clear l_carrid_last.

    endif.

    Carrier nodes:

    (always inserted as child of the last month

    which is identified by 'l_month_key')

    if l_carrid <> l_carrid_last.

    l_carrid_last = l_carrid.

    perform add_carrid_line using ls_sflight

    l_month_key

    changing l_carrid_key.

    endif.

    Leaf:

    (always inserted as child of the last carrier

    which is identified by 'l_carrid_key')

    perform add_complete_line using ls_sflight

    l_carrid_key

    changing l_last_key.

    endloop.

    endform. " create_hierarchy

    &----

    -


    *& Form add_month

    &----

    -


    text

    -

    -


    form add_month using p_yyyymm type c

    p_relat_key type lvc_nkey

    changing p_node_key type lvc_nkey.

    data: l_node_text type lvc_value,

    ls_sflight type sflight,

    l_month(15) type c.

    get month name for node text

    perform get_month using p_yyyymm

    changing l_month.

    l_node_text = l_month.

    add node:

    ALV Tree firstly inserts this node as a leaf if you do not provide

    IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'

    the leaf gets a child and thus ALV converts it to a folder

    automatically.

    call method g_alv_tree->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 = ls_sflight

    importing

    e_new_node_key = p_node_key.

    endform. " get_month

    &----

    -


    *& Form get_month

    &----

    -


    text

    -

    -


    -->P_P_YYYYMM text

    <--P_L_MONTH text

    -

    -


    form get_month using p_yyyymm

    changing p_month.

    Returns the name of month according to the digits in p_yyyymm

    data: l_monthdights(2) type c.

    l_monthdights = p_yyyymm+4(2).

    case l_monthdights.

    when '01'.

    p_month = 'January'.

    when '02'.

    p_month = 'February'.

    when '03'.

    p_month = 'March'.

    when '04'.

    p_month = 'April'.

    when '05'.

    p_month = 'May'.

    when '06'.

    p_month = 'June'.

    when '07'.

    p_month = 'July'.

    when '08'.

    p_month = 'August'.

    when '09'.

    p_month = 'September'.

    when '10'.

    p_month = 'October'.

    when '11'.

    p_month = 'November'.

    when '12'.

    p_month = 'December'.

    endcase.

    concatenate p_yyyymm+0(4) '->' p_month into p_month.

    endform. " get_month

    &----

    -


    *& Form add_carrid_line

    &----

    -


    text

    -

    -


    -->P_LS_SFLIGHT text

    -->P_L_MONTH_KEY text

    <--P_L_CARRID_KEY text

    -

    -


    form add_carrid_line using ps_sflight type sflight

    p_relat_key type lvc_nkey

    changing p_node_key type lvc_nkey.

    data: l_node_text type lvc_value,

    ls_sflight type sflight.

    add node

    ALV Tree firstly inserts this node as a leaf if you do not provide

    IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'

    the leaf gets a child and thus ALV converts it to a folder

    automatically.

    l_node_text = ps_sflight-carrid.

    call method g_alv_tree->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 = ls_sflight

    importing

    e_new_node_key = p_node_key.

    endform. " add_carrid_line

    &----

    -


    *& Form add_complete_line

    &----

    -


    text

    -

    -


    form add_complete_line using ps_sflight type sflight

    p_relat_key type lvc_nkey

    changing p_node_key type lvc_nkey.

    data: l_node_text type lvc_value.

    write ps_sflight-fldate to l_node_text mm/dd/yyyy.

    add leaf:

    ALV Tree firstly inserts this node as a leaf if you do not provide

    IS_NODE_LAYOUT with field ISFOLDER set.

    Since these nodes will never get children they stay leaves

    (as intended).

    *

    call method g_alv_tree->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 = ps_sflight

    importing

    e_new_node_key = p_node_key.

    endform. " add_complete_line

    &----

    -


    *& Module USER_COMMAND_0100 INPUT

    &----

    -


    text

    -

    -


    module user_command_0100 input.

    case sy-ucomm.

    when 'EXIT'.

    leave program.

    endcase.

    endmodule. " USER_COMMAND_0100 INPUT

    reward if useful

    cheers,

    rekha

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 10, 2008 at 06:59 AM

    Try using standard programs:

    1) BCALV_GRID_DND_TREE_SIMPLE

    2) BCALV_TREE_DEMO

    Reward if useful.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 10, 2008 at 07:11 AM

    hi,,

    try this,,

    REPORT YKRISS_TREE_ALV .

    • Type pools Declarations

    type-pools : slis .

    • Tables Declarations

    tables : vbak ,

    vbap .

    • Internal tables declarations

    data : begin of it_vbak occurs 0 ,

    vbeln like vbak-vbeln ,

    erdat like vbak-erdat ,

    erzet like vbak-erzet ,

    vkorg like vbak-vkorg ,

    end of it_vbak .

    data : begin of it_vbap occurs 0 ,

    vbeln like vbap-vbeln ,

    posnr like vbap-posnr ,

    matnr like vbap-matnr ,

    kwmeng like vbap-kwmeng ,

    vrkme like vbap-vrkme ,

    end of it_vbap .

    data : begin of it_mard occurs 0 ,

    matnr like mard-matnr ,

    werks like mard-werks ,

    lgort like mard-lgort ,

    end of it_mard .

    • Final Internal table for ALV .

    data : begin of it_final occurs 0 ,

    vbeln like vbak-vbeln ,

    erdat like vbak-erdat ,

    erzet like vbak-erzet ,

    vkorg like vbak-vkorg ,

    posnr like vbap-posnr ,

    matnr like vbap-matnr ,

    kwmeng like vbap-kwmeng ,

    vrkme like vbap-vrkme ,

    werks like mard-werks ,

    lgort like mard-lgort ,

    end of it_final .

    • Internal table to store fieldcatalog

    data : it_fieldcat type slis_t_fieldcat_alv .

    • Internal table for Events

    data : it_events type slis_t_event.

    data : x_events type slis_alv_event .

    • Internal table to Build Treenode

    data : it_node type standard table of snodetext with header line .

    • Variables Declarations

    data : v_repid like sy-repid ,

    fg_check ,

    v_id type snodetext-id ,

    v_name type snodetext-name .

    • Constants Declarations

    constants : c_top(11) value 'TOP_OF_PAGE', " For event

    c_t(3) value 'TOP'. " For form name

    • Selection Screen Layout

    selection-screen begin of block b1 with frame title text-001 .

    select-options : s_vbeln for vbak-vbeln visible length 4.

    parameters : p_alv radiobutton group rad default 'X',

    p_tree radiobutton group rad .

    selection-screen end of block b1 .

    ************************************************************************

    • Initialization.

    ************************************************************************

    initialization .

    v_repid = sy-repid .

    ************************************************************************

    • At Selection Screen

    ************************************************************************

    at selection-screen .

    • Perform Validate Input

    perform validate_input .

    ************************************************************************

    • Start Of Selection

    ************************************************************************

    start-of-selection .

    • Perform to Get data .

    perform get_data .

    ************************************************************************

    • End Of Selection

    ************************************************************************

    end-of-selection .

    if fg_check <> 'X'.

    if p_alv = 'X' .

    • Perform to display alv .

    perform alv_display .

    elseif p_tree = 'X' .

    • Perform to display Tree .

    perform tree_display .

    endif .

    else .

    message i064(zt).

    endif .

    &----


    *& Form get_data

    &----


    • Perform to Extract Data

    ----


    form get_data.

    select vbeln

    erdat

    erzet

    vkorg

    from vbak

    into table it_vbak

    where vbeln in s_vbeln .

    if not it_vbak[] is initial .

    sort it_vbak.

    select vbeln

    posnr

    matnr

    kwmeng

    vrkme

    from vbap

    into table it_vbap

    for all entries in it_vbak

    where vbeln = it_vbak-vbeln .

    else .

    fg_check = 'X' .

    stop .

    endif .

    if not it_vbap[] is initial .

    sort it_vbap .

    select matnr

    werks

    lgort

    from mard

    into table it_mard

    for all entries in it_vbap

    where matnr = it_vbap-matnr .

    if sy-subrc = 0 .

    sort it_mard .

    endif .

    else .

    fg_check = 'X' .

    stop .

    endif .

    endform. " get_data

    &----


    *& Form alv_display

    &----


    • Perform to Display ALV

    ----


    form alv_display.

    loop at it_vbak .

    loop at it_vbap where vbeln = it_vbak-vbeln .

    clear : it_final .

    it_final-vbeln = it_vbak-vbeln .

    it_final-erdat = it_vbak-erdat .

    it_final-erzet = it_vbak-erzet .

    it_final-vkorg = it_vbak-vkorg .

    it_final-posnr = it_vbap-posnr .

    it_final-matnr = it_vbap-matnr .

    it_final-kwmeng = it_vbap-kwmeng .

    it_final-vrkme = it_vbap-vrkme .

    read table it_mard with key matnr = it_vbap-matnr .

    if sy-subrc = 0 .

    it_final-werks = it_mard-werks .

    it_final-lgort = it_mard-lgort .

    endif .

    append it_final .

    endloop .

    endloop .

    • Perform to populate Field Catalog

    perform get_fieldcat .

    • Perform to populate events .

    perform get_events .

    • Display the report using ALV

    perform display_alv.

    endform. " alv_display

    &----


    *& Form get_fieldcat

    &----


    • To Populate Fieldcatalog

    ----


    form get_fieldcat.

    refresh it_fieldcat.

    clear it_fieldcat.

    call function 'REUSE_ALV_FIELDCATALOG_MERGE'

    exporting

    i_program_name = v_repid

    i_internal_tabname = 'IT_FINAL'

    i_inclname = v_repid

    changing

    ct_fieldcat = it_fieldcat

    exceptions

    inconsistent_interface = 1

    program_error = 2

    others = 3.

    endform. " get_fieldcat

    &----


    *& Form get_events

    &----


    • To Populate Events

    ----


    form get_events.

    x_events-name = c_top.

    x_events-form = c_t.

    append x_events to it_events.

    clear x_events.

    endform. " get_events

    &----


    *& Form display_alv

    &----


    • Perform to Display ALV

    ----


    form display_alv.

    call function 'REUSE_ALV_LIST_DISPLAY'

    exporting

    i_callback_program = v_repid

    it_fieldcat = it_fieldcat

    it_events = it_events

    tables

    t_outtab = it_final

    exceptions

    program_error = 1

    others = 2

    .

    if sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    endif.

    endform. " display_alv

    &----


    *& Form tree_display

    &----


    • Perform to Display Tree report

    ----


    form tree_display.

    data : l_text like snodetext-text ,

    l_date(10) ,

    l_time(8) ,

    l_kwmeng(13) .

    • perform to display header of the tree .

    perform add_node using v_id 'HEADER' 1 'Sales Documents' 16 3 .

    loop at it_vbak .

    clear : v_id ,l_text ,v_name.

    v_id = sy-tabix .

    write : it_vbak-erdat to l_date ,

    it_vbak-erzet to l_time .

    concatenate it_vbak-vbeln

    l_date

    l_time

    it_vbak-vkorg

    into l_text

    separated by space .

    v_name = it_vbak-vbeln .

    • Perform to add node level 2

    perform add_node using v_id v_name 2 l_text 35 4 .

    loop at it_vbap where vbeln = it_vbak-vbeln .

    clear : v_id ,l_text,v_name ,l_kwmeng.

    v_id = sy-tabix .

    write it_vbap-kwmeng to l_kwmeng .

    concatenate it_vbap-posnr

    it_vbap-matnr

    l_kwmeng

    it_vbap-vrkme

    into l_text

    separated by space .

    v_name = it_vbap-posnr .

    • Perform to add node level 3 .

    perform add_node using v_id v_name 3 l_text 40 7 .

    clear : v_id ,l_text ,v_name.

    read table it_mard with key matnr = it_vbap-matnr .

    if sy-subrc = 0 .

    v_id = sy-tabix .

    concatenate it_mard-werks

    it_mard-lgort

    into l_text

    separated by space .

    v_name = it_mard-matnr .

    • Perform to add level 4 .

    perform add_node using v_id v_name 4 l_text 20 2 .

    endif .

    endloop .

    endloop .

    • Call Function rs_tree_construct to build the tree view

    call function 'RS_TREE_CONSTRUCT'

    exporting

    insert_id = '000000'

    relationship = ' '

    tables

    nodetab = it_node

    exceptions

    tree_failure = 1

    id_not_found = 2

    wrong_relationship = 3

    others = 4

    .

    if sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    endif.

    • Call Function to expand Tree

    call function 'RS_TREE_EXPAND'

    exporting

    node_id = it_node-id

    all = ' '

    exceptions

    not_found = 1

    others = 2

    .

    if sy-subrc <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    endif.

    • Call Function RS_TREE_LIST_DISPLAY to Display Tree View

    call function 'RS_TREE_LIST_DISPLAY'

    exporting

    callback_program = v_repid.

    endform. " tree_display

    &----


    *& Form TOP

    &----


    • Form for Top of page

    ----


    form top.

    write :/ 'Program :', sy-repid ,

    25 'Date :' , sy-datum,

    45 'Time :' , sy-uzeit.

    endform .

    &----


    *& Form add_node

    &----


    • Populate IT_NODE internal table

    ----


    • -->P_V_ID text

    • -->P_0461 text

    • -->P_1 text

    • -->P_0463 text

    • -->P_18 text

    • -->P_1 text

    ----


    form add_node using p_id like snodetext-id

    p_name like snodetext-name

    p_tlevel like snodetext-tlevel

    p_text like snodetext-text

    p_length like snodetext-tlength

    p_color like snodetext-tcolor .

    it_node-id = p_id.

    it_node-name = p_name.

    it_node-tlevel = p_tlevel.

    it_node-text = p_text.

    it_node-tlength = p_length.

    it_node-tcolor = p_color.

    append it_node.

    clear it_node.

    endform. " add_node

    &----


    *& Form validate_input

    &----


    • To Validate Sales Document

    ----


    FORM validate_input.

    select single vbeln

    from vbak

    into vbak-vbeln

    where vbeln in s_vbeln .

    if sy-subrc <> 0 .

    message e065(zt).

    endif .

    ENDFORM. " validate_input

    PLS REWARD IF USEFUL

    REGARDS,

    REKHA

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 10, 2008 at 07:26 AM

    Hi,

    Check the following link:

    http://sapdev.co.uk/reporting/alv/alvtree.htm

    Regards,

    Bhaskar

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.