View Full Version : Tree and the branch/leaf distinction

11-21-2008, 11:29 PM
Hi everybody,

I have a question that kept me busy for days. In my treeview, it is possible to move items. The Tree's dragEnabled, dropEnabled, and dragMoveEnabled are all set to true. This works perfectly fine.

Now, the default behavior of the flex Tree is that leafs can only be dragged to branches. It is not possible to drag a leaf to another leaf in order to make it a child of the target leaf.

I'd like to modify the behavior in such a way that each leaf can be a potential branch. By dragging a leaf to another leaf, the target leaf should turn into a branch and the dragged leaf should be the child of the target leaf.

I have tried many things. One of the solutions I investigated is to define each item in the tree as a branch. However, I encounter lots of problems with this solution. When a user expands a branch that does not have any leaves, he/she gets an error. An exception is also raised when the user tries to drag a child branch away from its parent.

I was wondering whether anybody here tried to do the same thing. If so, how did you solvethe problem? Any suggestions about how to approach this problem are welcome!

11-22-2008, 01:04 AM
I haven't tried this per say, but I would start by listening for the dragDrop event then cancel the default action with event.preventDefault() and event.stopImmidiatePropegation(). You would then manually add the event.dragSource to the event.target.data.children. I'm not 100% sure if this is how you would reference the data of the drop target, but a quick trace should tell you the component type which will tell you how to reference that data. Most likely by using the data property. IMHO, shifting data around in a tree structure is much easier if all of your node data is in the form of a strongly typed data class which has a reference to it's parent. Not sure how your model is implemented, but it will probably make your life easier if you're not doing this already.

Best Regards,