Do not allow node to be dropped at source location based on source location properties.
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
Never mind about this issue. I got it working by determining the source from the allowDrop after all.
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
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;
}
I think the full signature is:
allowDrop(element: any, to: { parent: TreeNode, index: number }, dragEvent: DragEvent)
elementis the dragged itemto.parentis actually theTreeNodereceiving 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.
@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.
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