【问题标题】:Angular duplicate item via Drag & Drop通过拖放的角度重复项目
【发布时间】:2020-06-07 18:39:43
【问题描述】:

场景

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

问题

查看示例后,它看起来非常简单。在drop-Event 中,我已将transferArrayItem() 替换为copyArrayItem(),但是我希望将拖动的项目保留在源列表中,即使 拖动也是如此。

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

你知道如何完成这种行为吗?

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

【问题讨论】:

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


    【解决方案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; 
                });
        }
    
      }
    

    【讨论】:

    • 非常感谢!通过 copyArrayItem() 复制项目不是问题。这已经奏效了。问题是在我的下拉列表中的拖动事件期间,该项目在源列表中消失了一小段时间。
    • 这个库中的拖动功能似乎是不可能的。要解决这部分问题,需要再创建一个事件。
    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多