I have a large tree of documents that I want to display in a dynamic
treeview, but I'm not sure how best to determine whether a given node
has children or not.
Each document has a 'path' element giving it's place in the tree, eg
this following doc has the parent 'AA', which in turn has the top-level
parent 'A'.
doc = {
'_id':'11203',
'path':['A','AA','11203']
...
};
Fetching and displaying the entire tree is simple... this view:
function(doc) { emit(doc.path, doc.display_name); }
emits in lexicographical order, and some php code turns it into a nested
list.
The entire tree is slow to retrieve and display in one go. With jquery
treeview-async, I retrieve and display only the top-level nodes on page
load, then on expanding a node it will call the server with the ID,
asking for children.
I have a view that returns just the immediate children of the given key:
function(doc) {
if (doc.doc_type=='group') {
//Emit the path of the parent...
doc.path.pop();
emit(doc.path, doc.display_name);
}
}
So things mostly work nicely - I can click on a node, and it will
dynamically load children, rinse, repeat.
It looks messy if the node has a little 'expand' icon next to it, though
- but there are no actual children below it. Is it possible to create a
view that will emit as value whether each node has children?
Cheers,
-Patrick Barnes