Skip to content

Instantly share code, notes, and snippets.

@krusli
Last active March 30, 2021 18:21
Show Gist options
  • Save krusli/a51ecd07fb84bbc9ae04eea2e37b14dc to your computer and use it in GitHub Desktop.
Save krusli/a51ecd07fb84bbc9ae04eea2e37b14dc to your computer and use it in GitHub Desktop.
<div class="row" cdkDropListGroup>
<!-- NOTE that each item gets its own cdkDropList, then we treat it as a series of different cdkDropLists. -->
<div *ngFor="let item of items; index as i"
class="col-6 col-md-4 col-lg-3 mb-3"
cdkDropList
[cdkDropListData]="i"
cdkDropListOrientation="horizontal">
<app-item [item]="item" cdkDrag (cdkDragEntered)="entered($event)" (cdkDragEnded)="ended($event)" [cdkDragData]="i">
</app-item>
</div>
</div>
lastFrom: number;
lastTo: number;
constructor(private cd: ChangeDetectorRef) { }
entered(e: CdkDragEnter) {
if (this.items && this.items.length === 1) {
return;
}
this.lastFrom = e.item.data;
this.lastTo = e.container.data;
}
ended(e: CdkDragEnd) {
if (this.items && this.items.length === 1) {
return;
}
if (this.lastFrom === undefined || this.lastTo === undefined) {
return;
}
moveItemInArray(this.items, this.lastFrom, this.lastTo);
this.cd.detectChanges();
}
@krusli
Copy link
Author

krusli commented Feb 7, 2019

Updated to handle some corner cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment