Angular PrimeNG Tree 拖拽

Angular PrimeNG Tree 拖拽

Angular PrimeNG是一个开源框架,它有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架用来做响应式网站,非常方便。在这篇文章中,我们将看到如何使用Angular PrimeNG中的Tree DragDrop。

Tree DragDrop在Tree组件中实现了拖放功能。节点可以在同一棵树内重新排序,并使用拖放功能在其他树之间转移。有2种Tree DragDrop。

  • 单一树:在这种情况下,节点将在一棵树内被重新排序。
  • 多树。在这种情况下,节点将在多个基于范围约束的树之间重新排序。

语法:

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

创建Angular应用程序和模块安装。

第1步:使用以下命令创建一个Angular应用程序。

ng new appname

第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。

cd appname

步骤3:在你给定的目录中安装PrimeNG。

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

项目结构:它将看起来像如下。

Angular PrimeNG Tree DragDrop

例子1:下面是一个简单的例子,演示Angular PrimeNG Tree DragDrop的使用。

  • app.component.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
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
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 { }

输出:

Angular PrimeNG Tree DragDrop

例子2:下面是另一个演示Angular PrimeNG Tree DragDrop使用的例子,我们在这里使用多个具有拖放功能的树。

  • app.component.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
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
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 { }

输出:

Angular PrimeNG Tree DragDrop

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程