primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Feature Request: Add drag and drop support to the tree

Open JakeSummers opened this issue 10 years ago • 32 comments

I am looking into how to add drag & drop support for the following two use cases:

  1. Drag an element from a Tree and drop it into another element in the Tree.
  2. Drag an element from a DataTabe and drop it into an element in the Tree.

At the moment PrimeNg has a Drag and Drop module that allows you to attach a draggable handle or droppable element onto an element. Unfortunately, I don't think I can use this with Trees because the treeNodes not exposed.

I think that I can make items in the DataTable draggable by using the templating functionality.

JakeSummers avatar Jul 29 '16 21:07 JakeSummers

I think that this may now be possible with PrimeNG 1.0.0-beta.13.

This release allows you to provide a template to render your nodes like this (using @ContentChildren):

<p-tree
    [value]="...">
    <div>some content</div> 
</p-tree>

I think that it may be possible to add a primeNg drag handle inside the ContentChildren. Haven't tried this yet, but I think this should work.

JakeSummers avatar Aug 24 '16 16:08 JakeSummers

hi jake,

i search a solution for "Drag an element from a Tree and drop it into another element in the Tree."

can you help?

cyberrranger avatar Oct 18 '16 07:10 cyberrranger

Hi @cyberrranger ,

Until this functionality is added into primeNg you can workaround by using something like this:

<p-tree
    [value]="displayedRootNodes"
    selectionMode="single"
    [selection]="selection"
>
    <template let-node pTemplate type="default">
        <span
            pDraggable="treedragdrop"
            pDroppable="treedragdrop"
            (onDragStart)="onDragStart($event, node)"
            (onDragEnd)="onDragEnd($event, node)"
            (onDrop)="onDrop($event, node)"
            (onDragEnter)="onDragEnter($event, node)"
            (onDragLeave)="onDragLeave($event, node)"
        >
            {{node.label}}
        </span>
    </template>
</p-tree>

Jake

JakeSummers avatar Oct 18 '16 14:10 JakeSummers

thx, is working

cyberrranger avatar Oct 19 '16 10:10 cyberrranger

Can you give the TypeScript too? My Drag and drop tree doesn't working...

VSmolyanski avatar Nov 03 '16 12:11 VSmolyanski

hi @vas4oo,

You have to implement the methods "onDrop, onDragStart, ..." yourself.

i work with a own rest api

onDragStart(event, node) {
        this.moveFile = node;
    }
    onDrop(event, target) {
        if (!this.moveFile || target.leaf) {
            return;
        }

        if (!this.moveFile.leaf) {
            // dont move root
            if (!this.moveFile.parent) {
                return;
            }

            if (target.parent == this.moveFile.uuid) {
                return;
            } else {
                // TODO path
            }

            this.adminService.moveGroup(this.moveFile.uuid, target.uuid);
        } else {
            this.adminService.moveItem(this.moveFile.uuid, this.moveFile.parent, target.uuid);
        }
    }

cyberrranger avatar Nov 03 '16 12:11 cyberrranger

I have made them but still not working...

VSmolyanski avatar Nov 03 '16 12:11 VSmolyanski

You need to remove and add it in the tree itself

cyberrranger avatar Nov 03 '16 12:11 cyberrranger

@cyberrranger Hello, I'm hoping that you can help me get this to work in my project. I have (onDragStart)="onDragStart($event, node)" on my tree template and onDragStart($event, node): void { this.selectedEquipment = node; console.log('onDragStart'); console.log(this.selectedEquipment); } in my component. When I attempt to drag, the onDragStart never fires. Any ideas?

bryanhaines avatar Jan 05 '17 16:01 bryanhaines

@bryanhaines have you more example code?

my ts class

export class AdminHierarchy extends PARENT { moveFile: Hierarchy; onDragStart(event, node) { this.moveFile = node; } } html show answer from @JakeSummers from 18 Oct 2016

cyberrranger avatar Jan 05 '17 16:01 cyberrranger

@cyberrranger Thanks for getting back to me. Here is my ts file: export class BOMTreeComponent { @Input() set ID(ID: string) { this.buildTree(ID); }

public equipmentLibrary: any[];
public selectedEquipment = {};

constructor(private bomTreeService: BOMTreeService) {
    //this.designId = this.sessionCacheService.lastSelectedDesignId;
}

onSelect($event): void {
    if ($event.node.leaf === false) {
        return;
    }
    this.selectedEquipment = $event.node;
    console.log(this.selectedEquipment);
}

onDragStart($event, node): void {
    if (node.leaf === false) {
        return;
    }
    this.selectedEquipment = node;
    console.log('onDragStart');
    console.log(this.selectedEquipment);
}

onDragEnd($event, node): void {
    console.log('onDragEnd');
}

onDrop($event, node): void {
    console.log('onDrop');
}

onDragEnter($event, node): void {
    console.log('onDragEnter');
}

onDragLeave($event, node): void {
    console.log('onDragLeave');
}

}

Here is my template: <p-tree [style]="{'max-height': '400px', 'overflow':'auto'}" [value]="equipmentLibrary" selectionMode="single" [(selection)]="selectedEquipment" (onNodeSelect)="onSelect($event)"> //

bryanhaines avatar Jan 05 '17 16:01 bryanhaines

you have no template

example: `<p-tree [value]="displayedRootNodes" selectionMode="single" [selection]="selection"

<template let-node pTemplate type="default">
    <span
        pDraggable="treedragdrop"
        pDroppable="treedragdrop"
        (onDragStart)="onDragStart($event, node)"
        (onDragEnd)="onDragEnd($event, node)"
        (onDrop)="onDrop($event, node)"
        (onDragEnter)="onDragEnter($event, node)"
        (onDragLeave)="onDragLeave($event, node)"
    >
        {{node.label}}
    </span>
</template>

`

cyberrranger avatar Jan 10 '17 16:01 cyberrranger

@cyberrranger I really appreciate your help and your reply. Believe me when I say that I do have a template, but for some reason it didn't show up when I copied and pasted my markup. I just tried again in this reply and the same thing happened. I placed // where the opening and closing tags for my template are in my example, above. Here is a screen shot:

code template

bryanhaines avatar Jan 10 '17 20:01 bryanhaines

@bryanhaines Did you import the drag and drop module ?

import { DragDropModule } from 'primeng/components/dragdrop/dragdrop';

foxx9 avatar Jan 12 '17 09:01 foxx9

@foxx9 Thank you for your reply. My import statement was "import { TreeModule, TreeNode, Draggable, Droppable } from 'primeng/primeng';". I added the import statement that you suggested, but still am not able to get the onDragStart event to fire. It seems as though no event will fire on my leaf nodes. I thought there was maybe some error in my template, but I can't see what it might be.

The error was in another part of the project. Thank you to both who offered help.

bryanhaines avatar Jan 12 '17 20:01 bryanhaines

8th of February is the planned date for 2.0-Final release. Then we’ll start working on the new features such as drag drop for tree, accessibility and rtl improvements, better touch support, live scrolling for datatable, spreadsheet component, captcha and server side rendering.

http://blog.primefaces.org/?p=4332

cyberrranger avatar Feb 06 '17 13:02 cyberrranger

Working on it for 4.0.RC1.

cagataycivici avatar Mar 07 '17 14:03 cagataycivici

Available for 4.0.RC1

cagataycivici avatar Mar 08 '17 14:03 cagataycivici

DragDrop between multiple trees are also supported in addition to dragdrop within the same tree. They can choose if nodes can be draggable and/or droppable. So for example one tree can only give nodes whereas one can decided only to receive nodes. Also with draggableScope and droppableScope, a tree can decide whether to accept nodes from a certain tree.

screen shot 2017-03-09 at 23 21 25

cagataycivici avatar Mar 09 '17 20:03 cagataycivici

Can anyone tell me about the version of primeng in package.json for tree drag-drop? I am getting an error while importing "TreeDragDropService". Currently, i am using "primeng": "^2.0.5" .

mkseng avatar Mar 28 '17 09:03 mkseng