Ability to cancel dropping node from p-tree
I'm submitting a ... (check one with "x")
[ ] bug report => Search github for a similar issue or PR before submitting
[X] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Current behaviour When you subscribe to onNodeDrop event it's already too late to forbid dropping here for business logic.
Expected behaviour It could be great to be able to cancel drop event if we don't authorize node to be dropped here.
Minimal reproduction of the problem with instructions Simply use a p-tree in your template. Subscribe to onNodeDrop event and in the callback method, it's impossible to prevent dropping from happening.
What is the motivation/use case for changing the behaviour? My tree is actually composed of several depth levels and my business logic told me that a node of depth 3 can't be moved to a depth 2 node.
Please tell us about your environment: OSX, WebStorm, NPM
-
Angular version: 4.3.2
-
PrimeNG version: 4.1.2
-
Browser: all
-
Language: all
I am having the same issue.
I am also looking for the same solution...
Is there any updates related to this issue?
Same issue here, need to prevent drop of leaf onto another leaf
I am also looking for the same solution..
the same for me
Until its implemented, the workaround could be to override the allowDrop method of the Tree component and write your own implemented of the drop logic.
However you may (actually you should) want to use the drop logic covered in the original method and just add your bit.
I thought of extending the Tree component and write a custom Tree component but TreeModule does not export UITreeNode which is required by pTree.
Overriding might look like this:
- In the component where you have used
pTree, get theTreeviaViewChild
@ViewChild(Tree) tree: Tree;
- And then in the
constructor, orngOnInitor whatever suites your case,prototypetheallowDrop
Tree.prototype.allowDrop = (dragNode: any, dropNode: any, dragNodeScope: any): boolean => {
return this._overrideAllowDrop(dragNode, dropNode, dragNodeScope)
}
_overrideAllowDrop is used for brevity purpose.
Just return false to cancel the drop.
But this has the disadvantage that the drop logic should be on the UI side and if your logic require the backend, this workaround might not be of that much help.
Although the backend was involved in my case but the logic was simple so i moved it on the UI side and thus prototyping serves the purpose.
@cagataycivici, Any update on the issue?
I tried to figure out how to get the index of the dragNode and dropNode. for the dragNode - you can use this.tree.dragNodeIndex though I didn't find how to get it for the dropNode. In addition - I want to disable the option to drag nodes 'over' each other and I couldn't find how to do so. Did anyone had luck with that ? maybe @KapilSachdev ?
As per the implementation you may not get the dropNodeIndex. (I didn't used it in my case.)
What do you mean by disabling the option to drag nodes 'over' each other ?
(If the drag feature is enabled it will effect every node until explicitly disabled.)
My goal is to drag and drop to save level and same level only. For example :
| - Folder 1
| - Node 1
| - Node 2
| - Folder 1.1
| - Node 3
| - Folder 2
| - Node 4
| - Node 5
Node 1 and 2 can switch places but they can not be dragged to folder 1.1 or Folder 2 Node 1 can not be dragged "over" / "inside" Folder 1.1
found the solution for that. Just added this code :
UITreeNode.prototype.onDropNodeDragOver = function (event) {
return;
};
Thanks for sharing the solution, this way you can also customize the dragging with drop.
However, if it was only to disallow the drop but show dragging (you were also trying to not show the drag move as it look) one might also add custom levels to node data and use allowDrop.
{
id: 1,
label: "Folder 1",
level: 1,
children: [
{
id: 1,
label: "Node 1",
level: 2,
children: [
]
}
],
id: 1,
label: "Folder 2",
level: 1,
children: [...]
}
and return false on drop on the condition one is interested in, like in your case siblings are allowed else restricted.
great ! so now here is my code :
node_dropped(e: any) { // If parents are equals and not dropping on same node and dropIndex exist(not allowing to drop a folder inside a folder) if (e.dragNode.parent === e.dropNode.parent && e.dragNode !== e.dropNode && e.dropIndex) { e.accept(); } }
Hi Everyone
I faсed with a similar problem
I would like to cancel dropping for business logic, but faced with error 'TypeError: Cannot read property 'splice' of null'. my tree like this
<p-tree #tree
selectionMode="single"
droppableNodes="true"
draggableNodes="true"
[validateDrop]="true"
[contextMenu]="cm"
[value]="companyTree"
[(selection)]="selectedNode"
(onNodeSelect)="nodeSelect($event)"
(onNodeContextMenuSelect)="nodeContextMenuSelect($event)"
(onNodeDrop)="onDrop($event)">
and
onDrop(event) {
if (my_condition) {
event.accept();
} else {
....
}
}
and what I have
it throws in tree.js
UITreeNode.prototype.processNodeDrop = function (dragNode) {
var dragNodeIndex = this.tree.dragNodeIndex;
this.tree.dragNodeSubNodes.splice(dragNodeIndex, 1); // ---- this line throws error
can someone help me?
How to capture UITreeNode, I am always getting it as undefined
@cagataycivici, the parent of the dragnode is not accessible if the event is not accepted, can this be fixed?
Hi!
Since this keeps surfacing, I am posting my solution:
HTML:
<p-tree (onNodeDrop)="node_dropped($event)" .... /p-tree>
TS:
node_dropped(e) {
// If parents are equals
if (e.dragNode.parent === e.dropNode.parent) {
e.accept();
}
this.iniService.change_state(false);
}
Cheers 🍺