Search code examples
angulartypescriptangular-materialangular-cdkangular-dragdrop

Drag an drop with array of objects


I have two arrays of objects, an A and a B.

I have a kind of list of each one and I intend to drag the objects from A to B and from B to A.

My problem is that when I drag all objects from A to B or from B to A, when trying to pass an object to the empty list, it stops working.

drop(event: CdkDragDrop<string[]>) {
    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
      );
    }
  }

Solution

  • If the box-list get empty the height will be 0px of the div where the event is attached:

    The solution is to add a height property with 100% :

    style="height:100%"
    

    Try this :

    <div class="six" style=" height: 75%;">
      <div class="card-deck cardsZone">
        <div class="card myCards">
          <div class="card-body" style="overflow-y: auto;"  #activeList="cdkDropList"
          class="box-list" style="height:100%"
          cdkDropList
          cdkDropListOrientation="vertical"
          [cdkDropListData]="A"
          [cdkDropListConnectedTo]="[inactiveList]"
          (cdkDropListDropped)="drop($event)">
            <div *ngFor="let nw of A" cdkDrag>
            <div class="card mysmallCcards">             
              <div class="card-body">
                       <span>{{nw.name}}</span>         
              </div>
            </div>
            </div>
          </div>
        </div>
        <div class="card myCards">
          <div class="card-body" style="overflow-y: auto;" #inactiveList="cdkDropList"
          class="box-list" style="height:100%"
          cdkDropList
          cdkDropListOrientation="vertical"
          [cdkDropListData]="B"
          [cdkDropListConnectedTo]="[activeList]"
          (cdkDropListDropped)="drop($event)">
            <div *ngFor="let pr of B" cdkDrag>
            <div class="card mysmallCcards">
              <div class="card-body">
               <span>{{pr.name}}</span>
              </div>
            </div>
            </div>
          </div>
        </div>
      </div>
    </div>