[英]Angular Material drag n drop CDK remove element on list
How can I remove a list item?如何删除列表项? I tried removing the item from the array associated with the list, but this breaks the list altogether.
我尝试从与列表关联的数组中删除该项目,但这完全破坏了列表。
I use Angular (with Material) 9.0我使用 Angular(带材质)9.0
I have 2 lists.我有 2 个列表。 When I drag an item from the first list (sequenceOfSlides) to the second (listOfSlides), I delete it, and from the second to the first I copy it.
当我将一个项目从第一个列表 (sequenceOfSlides) 拖到第二个 (listOfSlides) 时,我将其删除,然后从第二个列表到第一个列表复制它。 The problem was that I removed the elements from the array associated with the list with its full replacement.
问题是我从与列表关联的数组中删除了元素,并完全替换了它。 With @Eliseo, I began to remove elements from the original array without changing the link to it.
使用@Eliseo,我开始从原始数组中删除元素而不更改指向它的链接。 Template:
模板:
<div class="row" cdkDropListGroup> <div class="col-6"> <div id="sequenceOfSlidesElement" cdkDropList [cdkDropListData]="sequenceOfSlides" class="example-list" (cdkDropListDropped)="drop($event)"> <div class="example-box" *ngFor="let item of sequenceOfSlides" cdkDrag> {{item.ruName}} </div> </div> </div> <div class="col-6"> <div id="listOfSlidesElement" cdkDropList [cdkDropListEnterPredicate]="noReturnPredicate" [cdkDropListData]="listOfSlides" class="example-list" (cdkDropListDropped)="drop($event)"> <div class="example-box" *ngFor="let item of listOfSlides" cdkDrag> {{item.ruName}} </div> </div> </div> </div>
Component:成分:
drop(event: CdkDragDrop<ISlide[]>) { if (event.previousContainer === event.container) { moveItemInArray(event.container.data, event.previousIndex, event.currentIndex); } else { if (event.container.id === 'sequenceOfSlidesElement') { copyArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex); } else { this.sequenceOfSlides.splice(event.previousIndex, 1); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.