Open In App

Angular PrimeNG Tree DragDrop

Last Updated : 03 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the Tree DragDrop in Angular PrimeNG. 

The Tree DragDrop enables drag and drops functionality in the Tree component. Nodes can be reordered within the same tree and transferred between other trees using drag and drop functionality. There are 2 kinds of Tree DragDrop:

  • Single Tree: In this case, the nodes will be reordered within a tree.
  • Multiple Trees: In this case, the node will be reordered between multiple trees that are based on scope constraints.

Syntax:

<p-tree
  [value]="..."
  [draggableNodes]="true"
  [droppableNodes]="true"
  draggableScope="scope"
  droppableScope="scope">
</p-tree>

 

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

Example 1: Below is a simple example demonstrating the use of the Angular PrimeNG Tree DragDrop.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tree DragDrop</h4>
<p-tree [value]="files1" 
        [draggableNodes]="true" 
        [droppableNodes]="true" 
        draggableScope="self" 
        droppableScope="self">
</p-tree>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
import { TreeDragDropService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [TreeDragDropService],
})
export class AppComponent {
    files1: TreeNode[] = [];
  
    constructor() { }
  
    ngOnInit() {
        this.files1 = [
            {
                label: 'A',
                children: [
                    {
                        label: 'B',
  
                        children: [
                            {
                                label: 'C',
                            },
                            {
                                label: 'D',
                            },
                        ],
                    },
                    {
                        label: 'E',
  
                        children: [
                            {
                                label: 'F',
                            },
                        ],
                    },
                ],
            },
            {
                label: 'G',
  
                children: [
                    {
                        label: 'H',
                    },
                    {
                        label: 'I',
                    },
                    {
                        label: 'J',
                    },
                ],
            },
        ];
    }
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TreeModule } from 'primeng/tree';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [],
})
export class AppModule { }


Output:

 

Example 2: Below is another example demonstrating the use of the Angular PrimeNG Tree DragDrop, where we are using multiple trees with drag-and-drop functionality.

  • app.component.html

HTML




<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tree DragDrop</h4>
<h4>File1 server</h4>
<p-tree [value]="files1" 
        [draggableNodes]="true"
        [droppableNodes]="true"
        draggableScope="scope1"
        droppableScope="scope2">
</p-tree>
<h4>File2 server</h4>
<p-tree [value]="files2" 
        [draggableNodes]="true" 
        [droppableNodes]="true" 
        draggableScope="scope2"
        droppableScope="scope1">
</p-tree>


  • app.component.ts

Javascript




import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/api';
import { TreeDragDropService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [TreeDragDropService],
})
export class AppComponent {
    files1: TreeNode[] = [];
    files2: TreeNode[] = [];
  
    constructor() { }
  
    ngOnInit() {
        this.files1 = [
            {
                label: 'A',
                children: [
                    {
                        label: 'B',
  
                        children: [
                            {
                                label: 'C',
                            },
                            {
                                label: 'D',
                            },
                        ],
                    },
                    {
                        label: 'E',
  
                        children: [
                            {
                                label: 'F',
                            },
                        ],
                    },
                ],
            },
            {
                label: 'G',
  
                children: [
                    {
                        label: 'H',
                    },
                    {
                        label: 'I',
                    },
                    {
                        label: 'J',
                    },
                ],
            },
        ];
        this.files2 = [
            {
                label: 'K',
                children: [
                    {
                        label: 'L',
  
                        children: [
                            {
                                label: 'M',
                            },
                            {
                                label: 'N',
                            },
                        ],
                    },
                    {
                        label: 'O',
  
                        children: [
                            {
                                label: 'P',
                            },
                        ],
                    },
                ],
            },
            {
                label: 'Q',
  
                children: [
                    {
                        label: 'R',
                    },
                    {
                        label: 'S',
                    },
                    {
                        label: 'T',
                    },
                ],
            },
        ];
    }
}


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TreeModule } from 'primeng/tree';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TreeModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    providers: [],
})
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/tree/dragdrop



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads