Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a bug in method SET_DATA_ROW of class CL_SALV_TREE?

Tags:

abap

I spotted some very strange behavior using hierarchical SALV (class CL_SALV_TREE).

If I use the set_data_row method directly after creating the node instance then I receive the correct hierarchy.

REPORT zzy.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main
        RAISING cx_salv_msg cx_salv_error.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA: lt_data TYPE STANDARD TABLE OF t000.
    cl_salv_tree=>factory(
      IMPORTING
        r_salv_tree = DATA(lo_salv_tree)
      CHANGING
        t_table = lt_data
    ).

    DATA(lo_tree_settings) = lo_salv_tree->get_tree_settings( ).
    lo_tree_settings->set_hierarchy_header( `Hierarchy` ).
    lo_tree_settings->set_hierarchy_size( 30 ).

    DATA(lo_nodes) = lo_salv_tree->get_nodes( ).
    DATA(lo_root_node) = lo_nodes->add_node(
      related_node = space
      relationship = if_salv_c_node_relation=>last_child
      data_row = VALUE t000( mandt = '100' )
      collapsed_icon = '@3S\QStatus: Collapsed@'
      expanded_icon = '@3T\QStatus: Expanded@'
      row_style = if_salv_c_tree_style=>emphasized_positive
      text = '100'
    ).
    lo_root_node->set_data_row( VALUE t000( mandt = '100' ) ).
    lo_root_node->get_hierarchy_item( )->set_icon( '@0V\QOK@' ).

    DATA(lo_node1) = lo_nodes->add_node(
      related_node = lo_root_node->get_key( )
      relationship = cl_gui_column_tree=>relat_last_child
      text = '200'
    ).
    lo_node1->set_data_row( VALUE t000( mandt = '200' ) ).
    DATA(lo_node2) = lo_nodes->add_node(
      related_node = lo_node1->get_key( )
      relationship = cl_gui_column_tree=>relat_last_child
      text = '300'
    ).
    lo_node2->set_data_row( VALUE t000( mandt = '300' ) ).

    DATA(lo_node3) = lo_nodes->add_node(
      related_node = lo_node2->get_key( )
      relationship = cl_gui_column_tree=>relat_last_child
      text = '400'
    ).
    lo_node3->set_data_row( VALUE t000( mandt = '400' ) ).

    lo_salv_tree->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  TRY .
    lcl_main=>main( ).
  CATCH cx_salv_msg cx_salv_error.
    ASSERT 0 = 1.
  ENDTRY.

Correct hierarchy

However if I use set_data_row method at the end of my main method, then the result is totally unexpected.

REPORT zzy.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main
        RAISING cx_salv_msg cx_salv_error.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA: lt_data TYPE STANDARD TABLE OF t000.
    cl_salv_tree=>factory(
      IMPORTING
        r_salv_tree = DATA(lo_salv_tree)
      CHANGING
        t_table = lt_data
    ).

    DATA(lo_tree_settings) = lo_salv_tree->get_tree_settings( ).
    lo_tree_settings->set_hierarchy_header( `Hierarchy` ).
    lo_tree_settings->set_hierarchy_size( 30 ).

    DATA(lo_nodes) = lo_salv_tree->get_nodes( ).
    DATA(lo_root_node) = lo_nodes->add_node(
      related_node = space
      relationship = if_salv_c_node_relation=>last_child
      data_row = VALUE t000( mandt = '100' )
      collapsed_icon = '@3S\QStatus: Collapsed@'
      expanded_icon = '@3T\QStatus: Expanded@'
      row_style = if_salv_c_tree_style=>emphasized_positive
      text = '100'
    ).
    lo_root_node->get_hierarchy_item( )->set_icon( '@0V\QOK@' ).

    DATA(lo_node1) = lo_nodes->add_node(
      related_node = lo_root_node->get_key( )
      relationship = cl_gui_column_tree=>relat_last_child
      text = '200'
    ).

    DATA(lo_node2) = lo_nodes->add_node(
      related_node = lo_node1->get_key( )
      relationship = cl_gui_column_tree=>relat_last_child
      text = '300'
    ).

    DATA(lo_node3) = lo_nodes->add_node(
      related_node = lo_node2->get_key( )
      relationship = cl_gui_column_tree=>relat_last_child
      text = '400'
    ).

    lo_root_node->set_data_row( VALUE t000( mandt = '100' ) ).
    lo_node1->set_data_row( VALUE t000( mandt = '200' ) ).
    lo_node2->set_data_row( VALUE t000( mandt = '300' ) ).
    lo_node3->set_data_row( VALUE t000( mandt = '400' ) ).

    lo_salv_tree->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  TRY .
    lcl_main=>main( ).
  CATCH cx_salv_msg cx_salv_error.
    ASSERT 0 = 1.
  ENDTRY.

Incorrect hierarchy

Is this a bug in this component? I could not find any documentation that would explain this strange behavior.

like image 226
Jagger Avatar asked Apr 23 '16 16:04

Jagger


1 Answers

By calling set_data_row method you overwrite all raw data, including child relations.

In the first example you always set mandt first, and add node later on. If you would do it the other way around, you would also get 'one level deep' tree.

like image 115
verdevelli Avatar answered Nov 11 '22 07:11

verdevelli