简体   繁体   English

如何从Angular2+的mat-tree中的drop事件获取mat-tree-node元素?

[英]How to get mat-tree-node element from drop event in the mat-tree in Angular2+?

I have a requirement where I need to implement drag and drop between two containers which have a tree-like structure.我有一个需求,我需要在具有树状结构的两个容器之间实现拖放。 One container is TODO, the Other is Done.一个容器是 TODO,另一个容器是 Done。 It's working but dropped data always getting attach to node 1 of the Container 2(Done) because I don't know how to find the dropped container node index?它正在工作,但删除的数据总是附加到容器 2(完成)的节点 1,因为我不知道如何找到删除的容器节点索引?

在此处输入图片说明

Here is code.这是代码。 Working demo can be found here工作演示可以在这里找到

import './polyfills';

import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MatNativeDateModule} from '@angular/material';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {DemoMaterialModule} from './material-module';

import {TreeFlatOverviewExample} from './app/tree-flat-overview-example';

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    DemoMaterialModule,
    MatNativeDateModule,
    ReactiveFormsModule,
  ],
  entryComponents: [TreeFlatOverviewExample],
  declarations: [TreeFlatOverviewExample],
  bootstrap: [TreeFlatOverviewExample],
  providers: []
})
export class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);

Drag "Other" and drop into "Done" container now open Node 1. you will be able to see the dropped element.将“其他”拖放到“完成”容器中,现在打开节点 1。您将能够看到放置的元素。

Requirement : I want to drag from TODO container and want to drop into the exact Node of the container 2. Quick help will be really appreciated.要求:我想从 TODO 容器中拖动并希望放入容器 2 的确切节点中。快速帮助将不胜感激。

Ok, Finally after some research I am able to achieve the requirement by using "div" and " cdkDropListGroup " property which connects multiple drop zone.好的,最后经过一些研究,我能够通过使用连接多个放置区的“div”和“ cdkDropListGroup ”属性来实现需求。

According to angular If you have an unknown number of connected drop lists, you can use the cdkDropListGroup directive to set up the connection automatically. Note that any new cdkDropList that is added under a group will be connected to all other lists automatically根据 angular If you have an unknown number of connected drop lists, you can use the cdkDropListGroup directive to set up the connection automatically. Note that any new cdkDropList that is added under a group will be connected to all other lists automatically If you have an unknown number of connected drop lists, you can use the cdkDropListGroup directive to set up the connection automatically. Note that any new cdkDropList that is added under a group will be connected to all other lists automatically

In the drop container, I can not use mat-tree because that requires a data-source property that can not be dynamically defined(As of now I am aware of).在放置容器中,我不能使用 mat-tree,因为它需要一个无法动态定义的数据源属性(截至目前我知道)。 whereas in my case I wanted multiple drop zones dynamically, So I used " div "而在我的情况下,我想要动态多个拖放区,所以我使用了“ div

Below is the working link, if someone requires this kind of implementation then refer to the below link.下面是工作链接,如果有人需要这种实现,请参考下面的链接。 I hope this will save someone time.我希望这会节省一些人的时间。

drag-n-drop-treelist拖放树列表

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM