Angular 7 Drag and Drop - Dynamically Create Drop Zones

后端 未结 6 722
无人及你
无人及你 2021-01-31 18:12

Is there a way to dynamically create drop zones? I\'m having some troubles with ngFor and cdkDropList.

Here is my first list and draggable elements:

           


        
6条回答
  •  心在旅途
    2021-01-31 18:42

    Source Link

    Demo Link

    For Dynamic Drag n Drop Lists, we can use ID instead of # Template variables

    app.component.html

    Week {{week.id}}
    Week {{week.id}} {{weekItem}}

    app.component.ts

    import { Component } from '@angular/core';
    import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
    
      weeks = [];
      connectedTo = [];
    
    
      constructor() {
        this.weeks = [
          {
            id: 'week-1',
            weeklist: [
              "item 1",
              "item 2",
              "item 3",
              "item 4",
              "item 5"
            ]
          }, {
            id: 'week-2',
            weeklist: [
              "item 1",
              "item 2",
              "item 3",
              "item 4",
              "item 5"
            ]
          }, {
            id: 'week-3',
            weeklist: [
              "item 1",
              "item 2",
              "item 3",
              "item 4",
              "item 5"
            ]
          }, {
            id: 'week-4',
            weeklist: [
              "item 1",
              "item 2",
              "item 3",
              "item 4",
              "item 5"
            ]
          },
        ];
        for (let week of this.weeks) {
          this.connectedTo.push(week.id);
        };
      }
    
      drop(event: CdkDragDrop) {
        if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
        } else {
          transferArrayItem(event.previousContainer.data,
            event.container.data,
            event.previousIndex,
            event.currentIndex);
        }
      }
    }
    

提交回复
热议问题