primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Ability to cancel dropping node from p-tree

Open ChamNouki opened this issue 9 years ago • 17 comments

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

ChamNouki avatar Aug 01 '17 14:08 ChamNouki

I am having the same issue.

Abdelghani90 avatar Oct 06 '17 14:10 Abdelghani90

I am also looking for the same solution...

lingarajsureban avatar Oct 11 '17 09:10 lingarajsureban

Is there any updates related to this issue?

Abdelghani90 avatar Nov 27 '17 21:11 Abdelghani90

Same issue here, need to prevent drop of leaf onto another leaf

zivlevy avatar Dec 27 '17 17:12 zivlevy

I am also looking for the same solution..

akila89 avatar Feb 13 '18 09:02 akila89

the same for me

HolyMacarony avatar May 03 '18 18:05 HolyMacarony

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 the Tree via ViewChild
  @ViewChild(Tree) tree: Tree;
  • And then in the constructor, or ngOnInit or whatever suites your case, prototype the allowDrop
    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?

KapilSachdev avatar Jul 21 '18 05:07 KapilSachdev

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 ?

mojo2405 avatar Sep 20 '18 16:09 mojo2405

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.)

KapilSachdev avatar Sep 21 '18 11:09 KapilSachdev

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

mojo2405 avatar Sep 23 '18 07:09 mojo2405

found the solution for that. Just added this code :

UITreeNode.prototype.onDropNodeDragOver = function (event) { return; };

mojo2405 avatar Sep 23 '18 07:09 mojo2405

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.

KapilSachdev avatar Sep 26 '18 05:09 KapilSachdev

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(); } }

mojo2405 avatar Dec 02 '18 12:12 mojo2405

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 image 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?

oleksiyo avatar Mar 14 '19 09:03 oleksiyo

How to capture UITreeNode, I am always getting it as undefined

MayankArora20 avatar Feb 19 '21 14:02 MayankArora20

@cagataycivici, the parent of the dragnode is not accessible if the event is not accepted, can this be fixed?

aidacam avatar Dec 19 '23 09:12 aidacam

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 🍺

mojo2405 avatar Apr 11 '25 06:04 mojo2405