How to show only the TreeView sign expand [+] if children exist

I developed an application that populates a tree of hierarchical data in a database.

I designed it to use lazy loading, so it only gets child nodes when node expands.

My problem is that, obviously, I don’t know if node has children unless I call the database and look. Currently, I have implemented a dummy child element node, so that the [+] icon appears for all nodes, then I delete this node dummy and get real child nodes in the BeforeExpand event.

This means that I get the [+] icon for nodes that don’t have child nodes, so the user clicks the expand icon and nothing shows that looks a little crappy.

What is the preffrred method for handling child nodes in a lazy load tree? If I make a call to the database to find out if there are child nodes, could I just load the child nodes and forget about lazy loading?

It seemed to me that I had to store the "HasChildren" flag in the database, so I can selectively create my dummy child node only for nodes that actually have child nodes.

Sorry for the incoherent, I'm very interested to see what other people think ...

+1
source share
3 answers

When you make a call, check for children with the node data:

 SELECT tp.*, ( SELECT 1 FROM table tc WHERE tc.parent = tp.id LIMIT 1 ) AS has_children FROM table tp 

You do not need to count, it can take a long time.

Just make sure that at least one child exists.

+5
source

modify your initial request to return all the same data, but also the number of children. when the number of children is not zero, display [+]. send your scheme and request for help

+1
source

My preferred solution to this problem is to implement a pre-ordered tree walk on your hierarchical dataset. See http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ for an example implementation.

The fact is that if you save the left and right values ​​for each node, then if the left value and the right value differ by more than one, then the node has children.

The only noticeable drawback of this approach is that you must actively maintain these left and right values ​​when changing the structure of nodes.

+1
source

All Articles