I created an image gallery and I intend to change the position between them. For this I am using the Drag & Drop cdk library.
My problem is that the exchange of images does not always happen correctly, sometimes I exchange the first image with the second and this exchange does not happen.
How can I have this fully functional, horizontally and vertically?
Is there a way Drag can only start if the image is stored in the mdc-image-list - with-text-protection class?
Thank you !
.HTML
<ul class="mdc-image-list my-image-list" style="padding-left: 10px;padding-right: 10px;" cdkDropList [cdkDropListData]="data"
(cdkDropListDropped)="drop($event)">
<ng-container *ngFor="let product of data; let j = index;">
<li class="mdc-image-list__item" cdkDrag>
<div class="mdc-image-list__image-aspect-container">
<ng-container *ngIf="product.image == null; else productImage">
<img src="" class="mdc-image-list__image imagenotfound">
</ng-container>
<ng-template #productImage>
<img [src]="product.image" class="mdc-image-list__image">
</ng-template>
</div>
<div class="mdc-image-list--with-text-protection">
<div class="mdc-image-list__supporting mdc-image-list__supporting">
<span class="mdc-image-list__label">{{product.name}}</span>
</div>
</div>
</li>
</ng-container>
</ul>
.ts
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
);
}
}