Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cdk drag and drop in a nested situation

This is the third question here on SO regarding "cdk dnd" and "nested"!

I did not quite get the hacky suggestion of the other SO question.

So here is a very basic and simple -> STACKBLITZ <- I've created. Everything works fine to the point where I interact with the nested elements inside the container.

When I try to sort just the nested elements, angular tries to sort the nested element with the container itself ... which creates unwanted behavior.

Does anyone have an idea how to solve this? I will further work this one tomorrow.

like image 912
Andre Elrico Avatar asked Jan 15 '19 00:01

Andre Elrico


1 Answers

If you're still looking for an answer to this you can use cdkDragBoundary to restrict where an element can be dragged. In your example it would involve:

  1. Adding a class to the div holding the nested list
  2. Adding the cdkDragBoundary attribute to the divs holding the time periods, targeting the class added in 1.

The HTML for the container component will look like this:

<div style="background-color=pink;">
  <div [cdkDropListData]="timePeriods" cdkDropList cdkDropListOrientation="horizontal"
    id="containerId" [cdkDropListConnectedTo]="['allDataId']" 
  (cdkDropListDropped)="drop($event)" class="example-list">

    <div class="example-box" cdkDragBoundary=".example-list" *ngFor="let timePeriod of timePeriods" cdkDrag>{{timePeriod}}</div>
  </div>
</div>

*Edited the class being used to target the container

like image 50
Borquaye Avatar answered Nov 08 '22 07:11

Borquaye