I have a tree grid which I want to refresh, but I need to keep its open state after it loads. Data is loaded into the tree grid using loadDataOnDemand set to true. The problem is that after I reset the grid (I call fetchData({}), without caching) and I call setOpenState only the nodes immediately below the first level are retrieved and shown.
So if I have a structure that goes 3 levels deep and I tell setOpenState to open nodes on the first level as well as the second level, only the first level nodes will be expanded.
will result in the tree opening the first level. If you call it again it will open the second level nodes as well. Is there a way around this behavior?
For setOpenState() to work, the folders to be opened must actually be loaded. You can achieve this by loading more of the tree up front - loadDataOnDemand allows you to return any depth of hierarchy rather than just one level at a time. Essentially, load all the folders for which you want openState to be able to preserved.
Note that there is no reasonable way for the Tree to do this for you - restoring the openState for a tree that is loaded one level at a time could require many, many fetches.
Yes, this makes sense. I was hoping though there was a way to force it to automatically perform those fetches, as I rarely have more than one or two top level tree elements open. Problem is the tree itself is huge so I cannot load it all at once.
I suggest though that this note be added to the setOpenState() documentation page as there is no mention there that the folders to be opened need to be loaded (or I am unable to find it).
Note that you don’t have to load the whole tree; loading just folders that are likely to be open is sufficient.
If you wanted to use setOpenState() to make more than one round of fetches, you can call it one, wait for dataArrived() to fire on the tree (this.data) and call it again - this will trigger another level of depth of fetching.
But yes, it’s a good idea to mention this in the docs, we’ll add that.
Then, add a DataArrivedHandler on the tree, and call setOpenState again. The DataArrivedHandler will then call itself repeatedly until there is no more data to load.
For trees that get a number of nodes passed to the setOpenState method, the tree can take upwards of a minute to expand to the specified levels. Since the nodes passed to the setOpenState method vary per instance of the tree, it doesn’t seem that we could reliably load more of the data up front. One node might expand down five levels, while another might only expand down one level, and another node may not be expanded at all.
Taking the example in this thread and adding to it a bit:
['3/8','/3','/1/6/9','/1/2','/1','/']
is there a way to say that I want to pre-load (behind the scenes) to each of the nodes contained in this path? That way, when the user clicks to expand the tree to that set of levels, the tree can load much faster.
Process the openState string on the server and provide TreeGrid.initialData as a partially loaded tree, or return the partially loaded tree as the first response.
Thanks for your quick response. I’m struggling to find an example of this for SmartGWT. Specifically, what is the structure of the parameter that gets passed to setInitialData? The documentation says it’s a TreeNode. Does this mean that I build a string constructed of all the nodes that I want to be shown?
I’m having another problem with this. Based on events in my layout, the nodes that I want to expand to in the tree changes. I was thinking that when that change event occurs, I could remove the tree from the layout, re-create it with the new initial data, and add the tree back to the layout. But since my tree is a final variable it won’t let me assign it to a new tree again.
Is there another way I could accomplish this?
Is there a way to reload the tree with a different intialData set?
I’m trying something like this, to refresh the tree and reset it’s initial data.
TreeNode[] updatedData = myTree.getUpdatedData();//This is my method that build the array of TreeNodes
myTree.getTree().unloadChildren(myTree.getTree().getRoot());
myTree.getTree().addList(updatedData, myTree.getTree().getRoot());
It’s adding the data to the tree but doesn’t seem to completely unload the data that was there previously.
I’m getting the following warning: “Adding node to tree with id property set to:1. A node with this ID is already present in this Tree - that node will be replaced.”
It also loses the hierarchy. The child node is not connected to the parent node.
There’s no point in unloading at root - just provide a new Tree.
Aside fron this, it’s unclear if the new nodes are really all supposed to be under that parent or actually under a mix of parents. If it’s the latter, use linkNodes(), not addList().
Finally, just to sanity check, you’re not using this every time you need to load more data are you? You should be using normal load on demand once you’ve provided the initialData (just once).
The way I had structured my code was making this more difficult than it needed to be. You are right - it is simpler and more correct to just provide a new Tree. I spent some time to update my code so that I could do this and it works perfectly.