通过拖放的角度重复项

时间:2020-06-07 18:39:43

标签: angular drag-and-drop angular-cdk

场景

在我当前的项目(Angular 8)中,我想使用Angular CDK拖放模块将项目从源列表添加到目标列表。互动之后,该项目仍应出现在来源列表中。

问题

查看示例后,它看起来非常简单。在drop事件中,我已将transferArrayItem()替换为copyArrayItem(),但是我想即使在 拖动时也将被拖动的项目保留在源列表中。

为了向您提供简化的概述,我提供了Google的一个官方示例,并更改了drop-Event的处理方式以说明问题(1)。

您是否知道如何完成此行为?

1)https://stackblitz.com/edit/angular-chaxhv

1 个答案:

答案 0 :(得分:1)

Demo尝试

drop(event: CdkDragDrop<string[]>) {
    var self=this;
    if (event.previousContainer === event.container) {    
      moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
    }
    else if(event.container.id<event.previousContainer.id){
      transferArrayItem(event.previousContainer.data,
                        event.container.data,
                        event.previousIndex,
                        event.currentIndex); 

       this.todo=this.todo.filter(function(item, pos){
                  return self.todo.indexOf(item)== pos; 
                });
    }
    else {
      copyArrayItem(event.previousContainer.data,
                        event.container.data,
                        event.previousIndex,
                        event.currentIndex); 
     this.done=this.done.filter(function(item, pos){
              return self.done.indexOf(item)== pos; 
            });
    }

  }
相关问题