cancel
Showing results for 
Search instead for 
Did you mean: 

How do I know the level when I click an item in the tree?

former_member207732
Participant
0 Kudos

Hi,

When I click a tree node in tree component, how do I know the level of this item?

E.g., if I click the root node, then I get the level number 1, if I click second level node, I get the number 2 etc.

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

athavanraja
Active Contributor
0 Kudos

if you need the level info when clicking the node you have to set the level values when filling the table TVIEW/TVIEW2 field TLEVEL with tree contents. Which you can read it later. the levels wont get filled/available automatically.

Regards

Raja

former_member207732
Participant
0 Kudos

Hi,

So it means I have to set the level id myself.

But my current case is that I have a table which includes 2 fields:parentid,childid. I can easily make a TVIEW table by setting the parentkey and childkey attributes. But it seems hard to calculate the level and depth of the tree.

So do you have any suggestion about this case?

Thanks a lot!

athavanraja
Active Contributor
0 Kudos

what does this tree holds. most of the hierarchies within R/3 (cost center , profit center, etc) when read using BAPIs or FM would return level as well, we were just using that.

Regards

Raja

former_member207732
Participant
0 Kudos

Hi,

This data comes from a xml file. Unfortunately it doesn't has level data.

Regards,

Long

athavanraja
Active Contributor
0 Kudos

is the XML represnets the same tree structure? if yes you could use some of XPATH functions along with XLS to work out the level.

Regards

Raja

Former Member
0 Kudos

HI,

Are you transferring the XML data to be stored in some internal table?

You can select the data after reading from the table into temporary internal tables and set the level(using ID) in the layout,based on the values of the table rows.

As the data does not contain level detail,then you'l have to set the level yourself and then can read it as suggested earlier.

Regards,

Siddhartha

Answers (2)

Answers (2)

daniel_humberg
Contributor
0 Kudos

Hi,

when you create the tree, you don't have to specify the level. It'S sufficient to set parentid and childid correctly.

But of course, if you want to read it from the tree-table later, you have to set it before.

An alternative is to write your own method that returns the level for a given childid. Try something like this:



level = get_level( childid ).

method get_level.
* 1) get parentid for childid into lv_parent_id 
* ...
* 2) call this method recursively
* IF current node is root
    rv_level = 1.
* ELSE
    rv_level = get_level( lv_parent_id ) + 1.
* ENDIF

endmethod.

Former Member
0 Kudos

Hi,

You can get to know the level of a tree by the ID of its node.

Like,what i did was,In the layout,


<htmlb:treeNode id      = "root"
                text    = "MRF LIST"
                isOpen  = "true">
   <htmlb:treeNode id      = "node1"
                   text    = "MRF's To Be Processed"
                   isOpen  = "true"
                   tooltip = "MRF's to do"
                   toggle  = "X">
<%data :tab_0 type ZDI_MRF.%>
   <%loop at tab_mrf_0 into tab_0.%>
      <htmlb:treeNode id     = "node1<%=sy-tabix%>"
                      text   = "<%=tab_0-MRF_NUM%>"
                      image  = "@02@"
                      onNodeClick="show">
       </htmlb:treeNode>
    <% endloop.%>

By this,on node click in onInputProcessing,we can process it as


if  event->id = 'myTree1' and event->event_type = 'click'.
     data : t_event type ref to cl_htmlb_event_tree.
     t_event ?=  event.
     temp = t_event->node.
   <b>  if strlen( temp ) ge '6'. </b>
     data : x type string ,
            tab type zdi_mrf.

   <b>  if temp ca 'node1%'. </b>
            split temp at '1' into x node1.
          endif.

By this you can get the length of the ID and hence get the level,say id strlen( temp ) = 4 ,then node = root ,if strlen( temp ) = 5 ,then level 1.

Maybe you can do something similar.

Hope this helps,

Regards,

Siddhartha