cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a Dynamic Tree when the depth and no. of nodes is unknown

Former Member
0 Kudos

Hello,

I am creating a SD Product Hierarchy using Web Dynpro Tree. The Requirement is : No of Nodes & the Depth of the hierarchy is unknown at design time. All the details are coming from the RFC.

So, How can I build a tree dynamically reflecting the dynamic depth & no.of nodes?. I have the "Creating WebDynpro Tree" tutorial with me, But unable to understand dynamic tree creation.

If somebody know how-to do it, Please let me know.

Thanks,

Sunita.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

The tree structure is created by context node elements and their recursive child nodes.

Let your context structure look like


Products (node, 0:n)
-- id (string)
-- Parts (recursive node -> Products)

Say you have a product p . Then you add parts to this product by adding elements to the "Parts" subnode of p which again is of type "Products" (recursion):


IProductsElement p = ...;
/* add parts to product p: */
IProductsElement part1 = p.nodeParts().createAndAddProductsElement();
part1.setId("p/1");
IProductsElement part2 = p.nodeParts().createAndAddProductsElement();
part2.setId("p/2");

To add further sub-parts, use the same functionality e.g. for part2:


IProductsElement subpart1OfPart2 = part2.nodeParts().createAndAddProductsElement();
subpart1OfPart2.setId("p/2/1");

Hope you get the idea.

Armin

monalisa_biswal
Contributor
0 Kudos

When you ate not sure of the no. of nodes in the tree, the approach is to create tree with the help of recursive node.

blog explains how to create recursive tree.

Former Member
0 Kudos

Monalisa,

Thanks for pointing out to a nice blog. If the no. of nodes is unknown, its easy. But how to handle the levels / depth of the tree dynamically.

In this blog he assumed there are 3 levels at designtime. What if, there are unknown levels and it cant be determined at design time.

Does anybody faced this scenario. Please help,

Thanks,

Sunita.

monalisa_biswal
Contributor
0 Kudos

Hi Sunita,

The number of nodes determines the depth/levels of tree.

You can modify the same code, which creates 3 levels in the tree to create dynamic tree.

// create first level in the tree

IPrivate<ViewName>.I<Tree Node>Element rootelement = wdContext.create<Tree Node>Element();

rootelement.setText("<Text>");

wdContext.node<Tree Node>().addElement(rootelement);

//create n levels in the tree

for(int i=1;i<n;i++)

{

IPrivate<ViewName>.I<Tree Node>Element childelement

= rootelement.node<RecursiveNode>().create<Tree Node>Element();

rootelement.node<RecursiveNode>().addElement(childelement);

childelement.setText("<Text>");

//change parent element

rootelement = childelement;

}

Hope it helps!

Former Member
0 Kudos

>

> Hi Sunita,

>

> The number of nodes determines the depth/levels of tree.

> You can modify the same code, which creates 3 levels in the tree to create dynamic tree.

> // create first level in the tree

> IPrivate<ViewName>.I<Tree Node>Element rootelement = wdContext.create<Tree Node>Element();

> rootelement.setText("<Text>");

> wdContext.node<Tree Node>().addElement(rootelement);

>

> //create n levels in the tree

> for(int i=1;i<n;i++)

> {

>

> IPrivate<ViewName>.I<Tree Node>Element childelement

> = rootelement.node<RecursiveNode>().create<Tree Node>Element();

> rootelement.node<RecursiveNode>().addElement(childelement);

> childelement.setText("<Text>");

> //change parent element

> rootelement = childelement;>

> }

> Hope it helps!

Monalisa,

This Sample looks very promising. I havent tried it yet. I will update the thread once I try. BTW, Could you please explain me the last line of code "rootelement = childelement".

Appreciate your help. Yes, its truly helpful. Thanks.

Sunita.