Repository navigation
Description
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:
- Drag item from
cdk-dropsource S to A - In
(dropped)handler, insert the dragged data item into A's data without removing it from S's data - Drag same item from S to
cdk-dropB - Problem:
(dropped)event hasevent.previousIndex = -1andevent.previousContaineriscdk-dropA instead ofcdk-dropS.
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
kanidjar commentedon Sep 13, 2018
Could my issue be related to yours? All i am trying is to "copy" new items to my dropzone.
#13077
arlowhite commentedon Sep 13, 2018
@kanidjar seems like the same issue
Look at the
(dropped)event propertiescontainer,containerIndex,previousContainer,previousIndexand compare them with what you expect. In my case some of these would be wrong after dragging an item a second time.kanidjar commentedon Sep 14, 2018
Confirmed. Using your workaround fixes my issue :)
SAkhil95 commentedon Sep 24, 2018
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.
eden6 commentedon Sep 24, 2018
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);crisbeto commentedon Sep 24, 2018
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?
SAkhil95 commentedon Sep 24, 2018
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. 👍
eden6 commentedon Sep 24, 2018
@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
droppedevent 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
crisbeto commentedon Sep 24, 2018
@SAkhil95 are you also copying the object in your data model after the element has been dropped?
SAkhil95 commentedon Sep 24, 2018
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.
eden6 commentedon Sep 24, 2018
Here's what I was talking about:
https://stackblitz.com/edit/angular-w5ftfu
SAkhil95 commentedon Sep 24, 2018
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...
crisbeto commentedon Sep 24, 2018
@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.
33 remaining items
nayfin commentedon Jan 30, 2019
@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.
Tommatheussen commentedon Jan 31, 2019
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 :)
parliament718 commentedon Feb 6, 2019
@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?
jayanlee commentedon Feb 19, 2019
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
abhijitpd commentedon Mar 14, 2019
Any updates on drag and drop copy requirement.
DonsWayo commentedon Mar 25, 2019
Like @jayanlee say, this is a stackblitz example with copy https://stackblitz.com/edit/angular-xjex4y
PetrosA commentedon Mar 26, 2019
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
rajibhasan11 commentedon May 2, 2019
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.
nimishar1 commentedon May 3, 2019
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.
rajibhasan11 commentedon May 7, 2019
Please provide similar behavior like https://jsfiddle.net/drzaus/mP8kY/ in cdk dnd
angular-automatic-lock-bot commentedon Sep 10, 2019
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.
feat(drag-utils): add utility function for cloning array items from o…
feat(drag-utils): add utility function for cloning array items from o…