Domanda

I am new to GWT and I am having problems creating a Tree whose child nodes grow dynamically. When my page loads the tree looks something like this:

ROOT_Node
    |
    |_Child 1
    |
    |_Child 2

When user clicks on Child 1 or Child 2, they expand like this:

ROOT_Node
    |
    |_Child 1
    |   |
    |   |_Child 1_1
    |   |_Child 1_2
    |
    |_Child 2

If the user clicks on Child 1_1, if there are any child nodes for Child 1_1 it will again expand. And all the nodes will grow dynamically. I have done some basic samples on GWT TreeCell and understood that it will not grow dynamically. So whenever the user is clicking on a child node I am removing the CellTree and adding a new CellTree. This is too bad. At least I should be able to write some code which can handle any number of child nodes and their child nodes.

I have gone through a few GWT tutorials including official GWT tutorials. But could not find examples related to this kind of problem. Tutorials only contain "if ( value instanceof SomeClass) then do this", add some kind of selection handler etc. If I use this approach I should write lots of if-then-else blocks.

Can anyone please help me fix this problem. Or is there any easy way to display the tree without using CellTree? Will lists help me?

È stato utile?

Soluzione

CellTree DOES support dynamic data.

You can use an AsyncDataProvider if you want to support lazy loading or the hierarchy is not known before hand.
Also there is a showcase example showing the use of dynamic data which can be found here.

The use of all the if (value instanceof SomeClass) is necessary if you want to use different Cells for each tree level (see showcase example).

If Child1 and Child1_2 are of the same type and are supposed to be rendered in the same way, you can use a different approach. Instead of checking if the object is a specific class you could check a field in the object which tells you about to which level in the tree it belongs or if your objects are linked together via references (i.e. child object has a reference to it's parent) you can also make use of this information to create your TreeViewModel

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top