-<div class="subtitle" [class.active]="formSubtitleVisible">
+<div cdkDropList
+ [cdkDropListData]="subtitle.Foods"
+ (cdkDropListDropped)="reorderList($event)"
+ class="subtitle"
+ [class.active]="formSubtitleVisible">
<h2>{{subtitle.Subtitle}}</h2>
<button class="button"
type="button"
(click)="saveSubtitle()">Speichern</button>
</form>
- <div cdkDropList (cdkDropListDropped)="reorderList($event)">
- <div *ngFor="let food of subtitle.Foods; let i = index" cdkDrag>
- <app-food [food]="food"></app-food>
- <button class="button"
- type="button"
- (click)="insertNewFood(i)">Einfügen</button>
- <button class="button"
- type="button"
- (click)="removeFood(i)">Löschen</button>
- </div>
+ <div *ngFor="let food of subtitle.Foods; let i = index" cdkDrag>
+ <app-food [food]="food"></app-food>
+ <button class="button"
+ type="button"
+ (click)="insertNewFood(i)">Einfügen</button>
+ <button class="button"
+ type="button"
+ (click)="removeFood(i)">Löschen</button>
</div>
</div>
import { Component, Input, OnInit } from '@angular/core';
-import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
+import { CdkDragDrop,
+ moveItemInArray,
+ transferArrayItem } from '@angular/cdk/drag-drop';
import { ISubtitle } from '../isubtitle';
import { IFood } from '../ifood';
public reorderList(event: CdkDragDrop<IFood[]>)
{
- moveItemInArray(this.subtitle.Foods,
- event.previousIndex,
- event.currentIndex);
+ 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);
+ }
}
}