Sei sulla pagina 1di 8

Developing a tree structure using CL_SALV_TREE object model

Below is an example of tree structure. Column Header

Branch Nodes Hierarchy Column

Leaf nodes Structure

Expanders

Nodes

Items

The tree structure is comprised of the following areas: 1) The structure: The structure is defined by the initial table that is specified with the FACTORY method of CL_SALV_TREE. It is displayed by the column header, which is the top row of the tree structure. Every node in a tree structure has exactly the structure that is defined in the initial table. 2) Hierarchy Column: The first column of the tree structure is always the hierarchy column. It is not part of the initial table. It contains the symbols that the user can use to display or hide lower levels of a node. Different icons can be used with nodes to distinguish whether or not they are branch or leaf nodes. By default the hierarchy column has no text. Text and tool tip can be assigned as shown below;

Data: lr_tree type ref to cl_salv_tree. Data: settings type ref to cl_salv_tree_settings. settings = lr_tree->get_tree_settings( ). settings->set_hierarchy_header( 'Result Node' ). settings->set_hierarchy_tooltip( 'Result Node' ).

3) Nodes: The actual entries in the tree structure are called nodes. Every node has the same structure as the initial table. Every node is uniquely identified by a node key. Below is the code for adding a node to a tree structure; Data: nodes type ref to cl_salv_nodes, Node type ref to cl_salv_node.
nodes = lr_tree->get_nodes( ). node = nodes->add_node( related_node = l_key data_row = <fs_tree> collapsed_icon = '@4G@' expanded_icon = '@4G@' text = im_text relationship = cl_gui_column_tree=>re lat_last_child ).

Node key for top node in the hierarchy (for example, Branch coverage here in this case) is always space. And for remaining nodes it has to take from the previous node as below Key = node->get_key( ). 4) Item : items are components of a node. Each node contains exactly as many items as are contained in the initial table. Below is the sample code to get the item of a node, and display it as a checkbox and enable it for selection. Data: item TYPE REF TO cl_salv_item.
item = node->get_item( 'CHECKBOX' ). Pass the field name corresponding to get a particular item of a node item->set_type( if_salv_c_item_type=>checkbox ). Set the display type as check box. item->set_checked( abap_true ). item->set_enabled( abap_true ). item->set_editable( abap_true ).

The output will be as shown below;

Example Program on building a tree structure using SALV.


Test data for the tree structure A AA AA AA AA AA AA AA AA AB AB AB AB AB AB B 11 11 11 11 12 12 12 12 11 11 11 11 12 12 C 011 011 012 012 011 011 012 012 011 011 012 012 011 011 D 7890 7899 7890 7899 7890 7899 7890 7899 7890 7899 7890 7899 7890 7899 E 7890 7899 7890 7899 7890 7890 7890 7899 7890 7899 7890 7899 7890 7890

AB AB

12 12

012 012

7890 7899

7890 7899

The structure cab be displayed as below

Below is the sample code for displaying the tree structure


REPORT ZDEMO_SALV_TREE.

TYPES: BEGIN OF ty_tree, a TYPE char2, b TYPE char2, c TYPE char3, d TYPE char4, e TYPE char4, END OF ty_tree.

DATA: lt_tree TYPE TABLE OF ty_tree, lt_tree_tmp TYPE TABLE OF ty_tree. DATA: lr_tree TYPE REF TO cl_salv_tree. DATA: ls_tree TYPE ty_tree. DATA: l_a_key TYPE lvc_nkey, l_b_key TYPE lvc_nkey, l_c_key TYPE lvc_nkey, l_line_key TYPE lvc_nkey. FIELD-SYMBOLS <fs_tree> TYPE ty_tree. *TRY. CALL METHOD cl_salv_tree=>factory IMPORTING r_salv_tree = lr_tree CHANGING t_table = lt_tree_tmp. * CATCH cx_salv_error . *ENDTRY.

LOOP AT lt_tree ASSIGNING <fs_tree>. ON CHANGE OF <fs_tree>-a. PERFORM add_a USING <fs_tree> '' CHANGING l_a_key. ENDON. ON CHANGE OF <fs_tree>-b. PERFORM add_b USING <fs_tree> l_a_key CHANGING l_b_key. ENDON. ON CHANGE OF <fs_tree>-c. IF NOT <fs_tree>-a = 'AA'. PERFORM add_c USING <fs_tree> l_b_key CHANGING l_c_key. else. l_c_key = l_b_key. ENDIF. ENDON. PERFORM add_line USING <fs_tree> l_c_key CHANGING l_line_key. ENDLOOP. DATA: lr_columns TYPE REF TO cl_salv_columns_tree. lr_columns = lr_tree->get_columns( ).

lr_columns->set_optimize( abap_true ). lr_tree->display( ). *&---------------------------------------------------------------------* *& Form ADD_A *&---------------------------------------------------------------------* FORM add_a USING p_fs_tree TYPE ty_tree p_key CHANGING p_l_a_key. DATA: nodes TYPE REF TO cl_salv_nodes, node TYPE REF TO cl_salv_node, item TYPE REF TO cl_salv_item. *... 0 working with nodes nodes = lr_tree->get_nodes( ). TRY. ... 0.1 add a new node ... 0.3 set the data for the nes node node = nodes->add_node( related_node = p_key data_row = p_fs_tree relationship = cl_gui_column_tree=>relat_last_c hild ). * * p_l_a_key = node->get_key( ). CATCH cx_salv_msg. ENDTRY. ENDFORM. " ADD_A *&---------------------------------------------------------------------* *& Form ADD_B *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_FS_TREE text * -->P_L_A_KEY text * -->P_L_B_KEY text *----------------------------------------------------------------------* FORM add_b USING p_fs_tree TYPE ty_tree p_l_a_key CHANGING p_l_b_key. DATA: nodes TYPE REF TO cl_salv_nodes, node TYPE REF TO cl_salv_node. nodes = lr_tree->get_nodes( ). TRY. node = nodes->add_node( related_node = p_l_a_key data_row = p_fs_tree

relationship = cl_gui_column_tree=>relat_last_c hild ). p_l_b_key = node->get_key( ). CATCH cx_salv_msg. ENDTRY. ENDFORM. " ADD_B *&---------------------------------------------------------------------* *& Form ADD_C *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_FS_TREE text * -->P_L_B_KEY text * -->P_L_C_KEY text *----------------------------------------------------------------------* FORM add_c USING p_fs_tree TYPE ty_tree p_l_b_key CHANGING p_l_c_key. DATA: nodes TYPE REF TO cl_salv_nodes, node TYPE REF TO cl_salv_node. nodes = lr_tree->get_nodes( ). TRY. node = nodes->add_node( related_node = p_l_b_key data_row = p_fs_tree relationship = cl_gui_column_tree=>relat_last_c hild ). p_l_c_key = node->get_key( ). CATCH cx_salv_msg. ENDTRY. ENDFORM. " ADD_C *&---------------------------------------------------------------------* *& Form ADD_LINE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_<FS_TREE> text * -->P_L_C_KEY text * <--P_L_LINE_KEY text *----------------------------------------------------------------------* FORM add_line USING p_fs_tree p_l_c_key CHANGING p_l_line_key. DATA: nodes TYPE REF TO cl_salv_nodes, node TYPE REF TO cl_salv_node.

nodes = lr_tree->get_nodes( ). TRY. node = nodes->add_node( related_node = p_l_c_key data_row = p_fs_tree relationship = cl_gui_column_tree=>relat_last_child ). p_l_line_key = node->get_key( ). CATCH cx_salv_msg. ENDTRY.

ENDFORM.

" ADD_LINE

Potrebbero piacerti anche