mbraak / jqTree

Tree widget for jQuery
https://mbraak.github.io/jqTree/
Apache License 2.0
1.02k stars 177 forks source link

Remember expanded nodes? #326

Closed johandanforth closed 9 years ago

johandanforth commented 9 years ago

I'm using a tree with lazy loading on a "navigation page", and when I select an item in the node I display a list of items for that node. When I select a specific item in this list, the whole web page loads up a details page for the selected item. When I click back to the "navigation page" the tree is reset and I only see the "root" nodes again and I have to expand the tree to get back "down" to the child node I'm interested in again. My question is - is it possible for the tree to "remember" how it looked? I'm sure it's possible to do :) Thanks for any help! /Johan

mbraak commented 9 years ago

Yes, this is a well-known problem. The tree can only expand a node if that node is loaded. And if you use lazy loading then not all nodes are loaded initially.

One partial solution is to load more levels initially. So, you could load 2 levels of nodes instead of 1.

Another solution is more difficult. You load n levels of tree data. Except for the selected node, you load all levels of data.

So, let's say node 'n10' is selected, and its a grandchild of node2.

Then your initial data looks like this:

-n1
-n2
  -n4
    -n10
-n3

Another detail, jqTree adds a node parameter to the json data request, which contains the id of the selected node.

For example:

/tree.json?node=10

This whole example is implemented in the django_mptt_admin project, which is a project in Django.

Greetings

johandanforth commented 9 years ago

Ok, thanks, right now I'm going to load the entire tree and use some (hopefully) smart caching and see how it works out.