0
votes

I'm trying to use the new Angular 7 CDK Drag and drop to move a list of elements in my site that are added dynamically.

Using this example, taken from angular docs examples and modified for my needs, there you can see that I have a list of elements with a droppable container.

The problem is that when the draggable elements are generated from another component, they do not take the parent droppable as the referenced droppable container to be used. Instead the droppable container property in the CDK Draggable object is null.

I've tried setting the cdkDragRootElement property from CdkDrag directive to check if somehow I could reference the parent element, but instead it makes the elements move whole parent element. This is not the expected behaviour.

Maybe am I missing a CDK Draggable property or in CDK Droppable container to reference the parent droppable list in my list elements?

Here is the "relevant" code in the example:

cdk-drag-drop-sorting-example.html

<div cdkDropList class="example-list">
  <app-test></app-test>
</div>

test/test.component.ts

import { Component, OnInit } from '@angular/core';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
  movies = [
    'Episode I - The Phantom Menace',
    'Episode II - Attack of the Clones',
    'Episode III - Revenge of the Sith',
    'Episode IV - A New Hope',
    'Episode V - The Empire Strikes Back',
    'Episode VI - Return of the Jedi',
    'Episode VII - The Force Awakens',
    'Episode VIII - The Last Jedi'
  ];

  constructor() { }

  ngOnInit() {
  }

  drop(event: CdkDragDrop<string[]>) {
    console.log(event);
    /*moveItemInArray(this.movies, event.previousIndex, event.currentIndex);*/
  }

}

test/test.component.html

<div class="example-box" *ngFor="let movie of movies" cdkDrag (cdkDragDropped)="drop($event)">
  {{movie}}
</div>

EDIT

Expected result:

cdk-drag-drop-sorting-example.html

<div cdkDropList class="example-list">
  <app-test></app-test>
</div>

test/test.component.html

<div class="example-box" *ngFor="..." cdkDrag cdkDropList=".example-list">
  {{movie}}
</div>

Go through parents to find the selector and check if it has a cdkDropList directive/instance and attach it to the cdkDrag.

2

2 Answers

0
votes

property cdkDropList must be on app-test - this is DOM element with list of your items

0
votes

The problem is when you add at component level, DOM not able to recognize the cdkdropList. There are two ways in which this can work.
1) If we move cdkDragList inside the test compoenent. Which is something obvious and you might not be interested.
2) you can use DragDrop Service to add drag list at parent. This functionality of using drag drop as service was released with Angular CDK 7.3.7.


Refer stackblitz https://stackblitz.com/edit/angular-ngtemplate-reorder-7i6uuk?file=src%2Fapp%2Fapp.component.ts for implementation of dragdrop using service.