Skip to content

cdk-drop: allow drag-and-drop copy behavior #13100

Closed

Description

@arlowhite

Bug, feature request, or proposal:

In some designs, you want to drag the same item repeatedly to multiple drop zones. In other words, the drag-and-drop system should support copy behavior as well as move behavior.

What is the expected behavior?

Developers should be able to choose whether a drag and drop acts as a move or a copy. This should be determined by the developer's implementation of the (dropped) handler and how they manipulate container data.

What is the current behavior?

<cdk-drop> seems to assume move behavior and has hidden state that prevents the developer from achieving copy behavior. In the (dropped) handler, if the developer leaves a container.data item in the previousContainer, it can be dragged again as expected. However, on the next drag-and-drop event.previousIndex = -1 and event.previousContainer is the wrong cdk-drop

To clarify:

  1. Drag item from cdk-drop source S to A
  2. In (dropped) handler, insert the dragged data item into A's data without removing it from S's data
  3. Drag same item from S to cdk-drop B
  4. Problem: (dropped) event has event.previousIndex = -1 and event.previousContainer is cdk-drop A instead of cdk-drop S.

What are the steps to reproduce?

Providing a StackBlitz reproduction is the best way to share your issue.

StackBlitz starter: https://goo.gl/wwnhMV

I can create a demo if needed.

What is the use-case or motivation for changing an existing behavior?

The current <cdk-drop> behavior is restrictive and doesn't allow for copy-drag designs.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular CLI: 6.2.1
Node: 8.11.4
OS: linux x64
Angular: 6.1.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.7.5
@angular-devkit/build-angular      0.7.5
@angular-devkit/build-optimizer    0.7.5
@angular-devkit/build-webpack      0.7.5
@angular-devkit/core               0.7.5
@angular-devkit/schematics         0.8.1
@angular/cdk                       6.4.7
@angular/cdk-experimental          6.4.7
@angular/cli                       6.2.1
@angular/flex-layout               6.0.0-beta.18
@angular/material                  6.4.7
@angular/material-moment-adapter   6.4.7
@ngtools/webpack                   6.1.5
@schematics/angular                0.8.1
@schematics/update                 0.8.1
rxjs                               6.2.2
typescript                         2.9.2
webpack                            4.9.2

Is there anything else we should know?

I worked-around this issue in my App by forcibly re-creating the <cdk-drop> within my template (toggle via *ngIf).

Also, in my app, I wanted to be able to drag items from the drop zones back to the source zone as a way of removing them. In this case, I remove the data item from the previousContainer.data but nothing in the destination container is changed. Again, I had issues with<cdk-drop> state and needed to apply my hack of re-generating the component.

Activity

  1. added
    feature
    on Sep 13, 2018
  2. kanidjar commented on Sep 13, 2018

    @kanidjar

    Could my issue be related to yours? All i am trying is to "copy" new items to my dropzone.

    #13077

  3. arlowhite commented on Sep 13, 2018

    @arlowhite
    Author

    @kanidjar seems like the same issue

    Look at the (dropped) event properties container, containerIndex, previousContainer, previousIndex and compare them with what you expect. In my case some of these would be wrong after dragging an item a second time.

  4. kanidjar commented on Sep 14, 2018

    @kanidjar

    Confirmed. Using your workaround fixes my issue :)

  5. SAkhil95 commented on Sep 24, 2018

    @SAkhil95

    I am also facing the same issue,
    I have 3 lists A,B and C where B and C will be my drop zones.
    If items are moved from A then I cannot reuse the same item get pushed to other List. So, I guess drag and copy feature is required for to satisfy my requirement.
    Any Suggestions / comments will be extremely helpful , Thank You.

  6. eden6 commented on Sep 24, 2018

    @eden6

    When dropped inside B or C, you could temporarily call a method inside A that "resets" the array of items being displayed inside a timeout of 0 ms.
    Something like:
    this.items= []; setTimeout(() => { this.items= this.sourceItems.slice(); }, 0);

  7. crisbeto commented on Sep 24, 2018

    @crisbeto
    Member

    We used to have an issue where an item that was dragged into another container wasn't being reverted, however that has been resolved in 7.0.0-beta.1. What other issues are you running into?

  8. SAkhil95 commented on Sep 24, 2018

    @SAkhil95

    Hey @crisbeto and @eden6 , Thanks for your response.
    I will take an example so that the issue I am facing will be conveyed in simple terms.
    I have all my employees list in List A, I want to drag drop employees who will be awarded bonuses in Quarter 1 to List B and employees who will be awarded bonuses in Quarter 2 to List C, but there are some employees who will receive in both Q1 & Q2. If I use drag and move feature, I can use the element in only 1 List among B or C . So, I require drag copy behavior instead of drag move so that I can clone Employee X in List B as well as List C. I don't want Employee X to be moved only to either of List B or List C making it not possible to use in both the Lists.

    Once again thanks a lot for your responses. 👍

  9. eden6 commented on Sep 24, 2018

    @eden6

    @SAkhil95 That's what I had understood. I don't know if a copy will be implemented, but in the meantime if you catch the dropped event on containers B and C, you can reset the employees list displayed in A in order to get fresh items to drag again.
    resetEmployeesList(){ this.employees= []; setTimeout(() => { this.employees= this.originalEmployeesList.slice(); }, 0); }
    Hope this helps

  10. crisbeto commented on Sep 24, 2018

    @crisbeto
    Member

    @SAkhil95 are you also copying the object in your data model after the element has been dropped?

  11. SAkhil95 commented on Sep 24, 2018

    @SAkhil95

    I have created a simple Stack blitz demo , I just want the items in List A to be copied to List B rather than move.
    Please find below:
    https://stackblitz.com/edit/angular-rxypv6
    Thank You.

  12. eden6 commented on Sep 24, 2018

    @eden6

    Here's what I was talking about:
    https://stackblitz.com/edit/angular-w5ftfu

  13. SAkhil95 commented on Sep 24, 2018

    @SAkhil95

    Yes @eden6 , this will certainly help me, Thanks a ton for the alternate solution mate.
    Thanks @crisbeto, issue is now resolved with alternate solution provided by @eden6 .
    Thank you both once again...

  14. crisbeto commented on Sep 24, 2018

    @crisbeto
    Member

    @eden6 you don't need to hack around it with timeouts and resetting the list. You just have to push the item to the new list without removing it from the old one: https://stackblitz.com/edit/angular-ymlmql. Closing the issue since this seems to work now.

  15. 33 remaining items

  16. nayfin commented on Jan 30, 2019

    @nayfin
    Contributor

    @Tommatheussen there is work in progress to enable custom drop-zones, these could selectively implement drop list methods and behaviors. See #14261. The issue is still open but looking at the current source, it seems like the required pieces are there. I plan attempting a custom drop-zone in a few weeks after finishing up some other stuff. I'll document if I'm successful and submit a PR to publish so that others can do the same. Then, hopefully we can expand the cdk to facilitate other common drag functionalities.

  17. Tommatheussen commented on Jan 31, 2019

    @Tommatheussen

    @Tommatheussen there is work in progress to enable custom drop-zones, these could selectively implement drop list methods and behaviors. See #14261. The issue is still open but looking at the current source, it seems like the required pieces are there. I plan attempting a custom drop-zone in a few weeks after finishing up some other stuff. I'll document if I'm successful and submit a PR to publish so that others can do the same. Then, hopefully we can expand the cdk to facilitate other common drag functionalities.

    That's awesome news, I'll keep an eye out for more details on this. As we have to ship our software in a week or 3, I'll keep my current implementation as is and adapt when it's easier supported on the cdk, thanks for the heads up already :)

  18. parliament718 commented on Feb 6, 2019

    @parliament718

    @Tommatheussen Thanks for the example, it really helped. There is an undesirable jitter that occurs when the element it removed and re-added to the DOM (between dragStart and dragMove). I haven't figured out how to get rid of that, it's ugly but not such a major issue.

    However, the only additional functionality I need is to be able to drag an element outside of its container to remove it. Has anybody figured this out? Even if you drag the element way outside any drop area, cdkDropListDropped still fires with the original container. It would be great if event.container was null if an element was not dropped into any container, but that is not the case.

    Does anybody have any ideas how to remove the element when it's dragged outside?

  19. jayanlee commented on Feb 19, 2019

    @jayanlee

    My suggestion for copy behavior would be to use "copyArrayItem" which is similar to "moveArrayItem". For more info see https://material.angular.io/cdk/drag-drop/api#functions

  20. abhijitpd commented on Mar 14, 2019

    @abhijitpd

    Any updates on drag and drop copy requirement.

  21. DonsWayo commented on Mar 25, 2019

    @DonsWayo

    Like @jayanlee say, this is a stackblitz example with copy https://stackblitz.com/edit/angular-xjex4y

  22. PetrosA commented on Mar 26, 2019

    @PetrosA

    Yes it copies the items correctly, but the UI is not fitting. In the drag event it pulls the item out of the sourceList and thats not the expected behavior. It will confuse the user.
    Some update to leave the copy item to his sourceList would be very helpful

  23. rajibhasan11 commented on May 2, 2019

    @rajibhasan11

    JQuery DnD have clone or copy mode, in CDK copy is working but drag preview is creating but the item is not showing in the actual list in UI until drag-drop operations completed.

  24. nimishar1 commented on May 3, 2019

    @nimishar1

    You are right @DonsWayo . But i have done a small quick fix for this.

    dragStart(event:CdkDragStart){
    const tempTodo = event.source.dropContainer.data.slice();
    const indexValue = tempTodo.indexOf(event.source.element.nativeElement.innerText);
    const movableValue = tempTodo.splice(indexValue,1).join();
    event.source.dropContainer.data.splice(indexValue, 0, movableValue);
    }
    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);
    }
    }

    Copy these functions to you ts code. But there is another catch here, the reason why copy functionality is like that because the element that is dragged might not know whether its going to be put in another droplist or same droplist. This quick fix here works only if you put it in another droplist. If you try to put it or swap inside same droplist it creates a redundant name of the draggable element.

  25. rajibhasan11 commented on May 7, 2019

    @rajibhasan11

    Please provide similar behavior like https://jsfiddle.net/drzaus/mP8kY/ in cdk dnd

  26. angular-automatic-lock-bot commented on Sep 10, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  27. locked and limited conversation to collaborators on Sep 10, 2019
  28. added
    needs: discussion
    on Mar 3, 2020
  29. added a commit that references this issue on Nov 5, 2026
    d733a96
  30. added a commit that references this issue on Nov 20, 2026
    e37d160
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

needs: discussion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions