0

Recently, I'm working on a scenario which allow user can drag item from one container into drop zone to store items. It would be a way like draw.io does.

However, the items in cdkDropList is sticky. It would not be able to move around.

<div class="left-panel">
        <h6>container</h6>
        <div class="widget-container" 
        cdkDropList
        [id]="'container'"
        [cdkDropListData]="customWidgets"
        [cdkDropListConnectedTo]="['container', 'artBoard']"
        >
            <div cdkDrag><svg height="50" width="50">
            <rect width="50" height="50" style="fill:rgb(0,0,255);stroke- width:3;stroke:rgb(0,0,0)" />
            </svg>
            </div>
        </div>
        
    </div>
<div class="middle-workspace">
        <div class="svg-container" style="width:300px;height:300px;border-style: dotted;" 
        cdkDropList 
        [id]="'artBoard'" 
        [cdkDropListData]="artBoardWidges" 
        [cdkDropListConnectedTo]="['container', 'artBoard']"
        (cdkDropListDropped)="onTalkDrop($event)"
        >
            <app-widget-rect cdkDragBoundary=".svg-container" *ngFor="let artBoardWidge of artBoardWidges"></app-widget-rect>
        </div>
    </div>

Updated 06/26-02:25 Thanks for replying from @FunkMonkey33

The following code below is my dragHandler, apparently it will copy the original item from its container to target container.

onTalkDrop(event: CdkDragDrop<Widget[]>) {
    // In case the destination container is different from the previous container, we
    // need to transfer the given task to the target data array. This happens if
    // a div has been dropped on a different dropList.
    if (event.previousContainer === event.container) {
      moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
    } else {
      copyArrayItem(event.previousContainer.data,
        event.container.data,
        event.previousIndex,
        event.currentIndex);
    }
  }
  • We will have to see your handlers to be able to see why this isn't working. If your handler doesn't properly deal with the moved item, i.e. remove it from one array and add it to the other, then visually it will look like the drag item is "sticky" as you put it. It will just go right back to it's original position. – FunkMonkey33 Jun 25 '21 at 18:16
  • any update on this task? – Ganesh Jangam Jul 03 '21 at 18:02
  • Hi @GaneshJangam, I realized that the only I need to do is adding a directive called *CdkDragPlaceholder. It'll make dragged item acted like a element. – Heng-Shiou Sheu Jul 05 '21 at 04:53
  • @Heng-ShiouSheu, can I get an application example on this directive? I cant seem to get it right. – ilpianoforte Sep 01 '21 at 11:04

0 Answers0