angular-tree-component icon indicating copy to clipboard operation
angular-tree-component copied to clipboard

Do not allow node to be dropped at source location based on source location properties.

Open jdewell opened this issue 9 years ago • 6 comments

Hi. Thanks for writing a nice tree for angular. Can you tell me the best way to not allow a node to be dropped on a target node? I thought maybe allowDrop but that is associated with the source node. (node to be dropped). I want to disallow drop of target if source node has certain properties.

Thanks

jdewell avatar Aug 25 '17 21:08 jdewell

Never mind about this issue. I got it working by determining the source from the allowDrop after all.

jdewell avatar Aug 28 '17 15:08 jdewell

Can you please elaborate more on how did you solve this issue? This is the function that I am using for allowDrop

allowDrop: (element: any, {parent, index}) => {});

The element is what is being dragged, parent is the parent of the object that is being dragged and index is the index of the element that is being dragged.

There is no way to get the target object, i.e. where the dragged object is supposed to be dropped.

Can you please explain how did you manage to get the target properties and prevent allowDrop based on that because I am facing the same problem.

Thank you

sulhome avatar Sep 06 '17 10:09 sulhome

Hi I am also facing a similar issue, I need the user to not be able to drag nodes out of it's parent node.

The below snippet is from the docs. It mentions "to.parent" but never gives an example of how to access the "to"

allowDrop: (element, { parent, index }) { // return true / false based on element, to.parent, to.index. e.g. return parent.hasChildren; }

ABobbyT avatar Jan 15 '19 12:01 ABobbyT

I think the full signature is: allowDrop(element: any, to: { parent: TreeNode, index: number }, dragEvent: DragEvent)

  • element is the dragged item
  • to.parent is actually the TreeNode receiving the drop that you want to test.

If the drop targets a node, then to.parent references that node. If the drop targets a dropSlot between nodes, then to.parent references the parent node containing that dropSlot (which could be the root of the treeview), and the index the 0-based index of the dropSlot targeted.

KeithGillette avatar Jan 15 '19 13:01 KeithGillette

@sulhome I don't understand what's missing. parent and index tell you exactly where the node should be dropped. In your case (only allow drop inside the same parent):

allowDrop: (element, { parent, index }) => element.parent === parent

@ABobbyT if you want to access to you can just do: allowDrop: (element, to) . But it won't give you any more data other than parent and index.

adamkleingit avatar Jan 15 '19 15:01 adamkleingit

Thanks that lead me to exactly what I was looking for. Hope this helps someone but this is what I ended up doing.

options = { allowDrag: (node) => node.isLeaf, allowDrop: (element: any, to: { parent, index: number }) => { console.log('allowDrop - from', element.parent.data); console.log('allowDrop - to', to.parent.data); return element.parent.data === to.parent.data; } };

*Sorry still new to this, not sure how to get that code to display nicely

ABobbyT avatar Jan 16 '19 13:01 ABobbyT