Angular - 可排序的嵌套拖放
创始人
2024-10-14 19:00:41
0

要实现Angular中的可排序的嵌套拖放,可以使用第三方库ngx-drag-drop。

首先,确保已经安装了ngx-drag-drop库:

npm install ngx-drag-drop

下面是一个示例代码,演示了如何使用ngx-drag-drop库实现可排序的嵌套拖放:

首先,在app.module.ts文件中导入DragDropModule:

import { DragDropModule } from 'ngx-drag-drop';

@NgModule({
  imports: [
    DragDropModule
  ],
  ...
})
export class AppModule { }

然后,在app.component.html文件中添加拖放区域的标记:

{{ item.name }}
{{ child.name }}

在app.component.ts文件中,定义要拖放的数据模型和拖放行为:

import { Component } from '@angular/core';
import { DragulaService } from 'ngx-drag-drop';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  items = [
    {
      name: 'Item 1',
      children: [
        {
          name: 'Child 1'
        },
        {
          name: 'Child 2'
        }
      ]
    },
    {
      name: 'Item 2',
      children: [
        {
          name: 'Child 3'
        },
        {
          name: 'Child 4'
        }
      ]
    }
  ];

  constructor(private dragulaService: DragulaService) {
    dragulaService.createGroup('nested-bag', {
      moves: (el, container, handle) => {
        // 只允许拖放box元素
        return handle.classList.contains('box');
      }
    });
  }
}

最后,在app.component.css文件中,定义拖放的样式:

.container {
  margin-top: 20px;
}

.box {
  padding: 10px;
  border: 1px solid #ccc;
  background-color: #f5f5f5;
  margin-bottom: 10px;
  cursor: move;
}

.nested-list {
  margin-left: 20px;
}

.nested-item {
  padding: 10px;
  border: 1px solid #ccc;
  background-color: #f5f5f5;
  margin-bottom: 10px;
  cursor: move;
}

这样,你就可以在Angular应用中使用ngx-drag-drop实现可排序的嵌套拖放。

相关内容

热门资讯

2026版技巧!hhpoker... 2026版技巧!hhpoker真能买到挂吗(透视)微乐手游辅助脚本平台(本来存在有辅助app)-哔哩...
此事迅速冲上热搜!hhpoke... 此事迅速冲上热搜!hhpoker外开挂靠谱吗(透视)微乐广西小程序(一贯真的有辅助工具)-哔哩哔哩1...
透视策略!wpk透视辅助方法(... 透视策略!wpk透视辅助方法(德普之星破解版)果然真的有辅助app(哔哩哔哩)1、用户打开应用后不用...
透视辅助!pokerworld... 您好,pokerworld破解版下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...
透视办法!wpk辅助(WePo... 透视办法!wpk辅助(WePoKer功能)一直有辅助软件(哔哩哔哩)wpk辅助破解侠是真的助透视。每...
据监测!wepoker私人局透... 据监测!wepoker私人局透视方法(透视)微乐小程序游戏破解器下载(好像是真的辅助app)-哔哩哔...
透视总结!wepokerplu... 透视总结!wepokerplus作必弊(HHpoker透视器)总是存在有辅助方法(哔哩哔哩)1、许多...
据玩家消息!hhpoker到底... 据玩家消息!hhpoker到底可以作必弊码(透视)微乐江苏自建房辅助app(真是是有辅助app)-哔...
透视窍要!xpoker辅助怎么... 透视窍要!xpoker辅助怎么用(德普之星开辅助)一直真的有辅助软件(哔哩哔哩)xpoker辅助怎么...
据悉!德州局透视脚本(透视)微... 据悉!德州局透视脚本(透视)微乐四川亲友圈辅助器(竟然真的是有辅助攻略)-哔哩哔哩1、德州局透视脚本...