How to use Angular7 (angular material) drag drop between two components
As recently angular introduced drag and drop in angular material
https://material.angular.io/cdk/drag-drop/overview .
All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.
angular drag-and-drop angular-material angular7 angular-cdk
add a comment |
As recently angular introduced drag and drop in angular material
https://material.angular.io/cdk/drag-drop/overview .
All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.
angular drag-and-drop angular-material angular7 angular-cdk
add a comment |
As recently angular introduced drag and drop in angular material
https://material.angular.io/cdk/drag-drop/overview .
All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.
angular drag-and-drop angular-material angular7 angular-cdk
As recently angular introduced drag and drop in angular material
https://material.angular.io/cdk/drag-drop/overview .
All examples describes with in a single component. How to use this in two different components, Drag one component item and drop into another component.
angular drag-and-drop angular-material angular7 angular-cdk
angular drag-and-drop angular-material angular7 angular-cdk
edited Nov 21 '18 at 13:47
Jomy Joseph
asked Nov 21 '18 at 13:31
Jomy JosephJomy Joseph
385
385
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You may use properties id and cdkDropListConnectedTo to link both lists:
Component 1:
<div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
Component 2:
<div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"
After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:
drop(event: CdkDragDrop<string>) {
if (event.container.id === event.previousContainer.id) {
// move inside same list
moveItemInArray(this.list, event.previousIndex, event.currentIndex);
} else {
// move between lists
}
}
For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.[cdkDropListConnectedTo]="[list-1]"
or[cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413175%2fhow-to-use-angular7-angular-material-drag-drop-between-two-components%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may use properties id and cdkDropListConnectedTo to link both lists:
Component 1:
<div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
Component 2:
<div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"
After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:
drop(event: CdkDragDrop<string>) {
if (event.container.id === event.previousContainer.id) {
// move inside same list
moveItemInArray(this.list, event.previousIndex, event.currentIndex);
} else {
// move between lists
}
}
For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.[cdkDropListConnectedTo]="[list-1]"
or[cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
|
show 1 more comment
You may use properties id and cdkDropListConnectedTo to link both lists:
Component 1:
<div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
Component 2:
<div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"
After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:
drop(event: CdkDragDrop<string>) {
if (event.container.id === event.previousContainer.id) {
// move inside same list
moveItemInArray(this.list, event.previousIndex, event.currentIndex);
} else {
// move between lists
}
}
For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.[cdkDropListConnectedTo]="[list-1]"
or[cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
|
show 1 more comment
You may use properties id and cdkDropListConnectedTo to link both lists:
Component 1:
<div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
Component 2:
<div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"
After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:
drop(event: CdkDragDrop<string>) {
if (event.container.id === event.previousContainer.id) {
// move inside same list
moveItemInArray(this.list, event.previousIndex, event.currentIndex);
} else {
// move between lists
}
}
For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.
You may use properties id and cdkDropListConnectedTo to link both lists:
Component 1:
<div cdkDropList id="list-1" cdkDropListConnectedTo="list-2" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
Component 2:
<div cdkDropList id="list-2" cdkDropListConnectedTo="list-1" (cdkDropListDropped)="drop($event)">
<div *ngFor="let item of list" cdkDrag>{{ item }}</div>
</div>
If you need to connect several lists to one list, you may use the following syntax: [cdkDropListConnectedTo]="['list-1', 'list-2', 'list-3', 'list-4']"
After linking the lists, you must correctly update one or both lists depending on the actions. You may do it on the drop function like this:
drop(event: CdkDragDrop<string>) {
if (event.container.id === event.previousContainer.id) {
// move inside same list
moveItemInArray(this.list, event.previousIndex, event.currentIndex);
} else {
// move between lists
}
}
For moving items between lists, you will possibly want to keep track of the lists centrally. You may do so by using a Service, a Store or other methods.
edited Nov 22 '18 at 15:46
answered Nov 22 '18 at 1:15
GCSDCGCSDC
535516
535516
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.[cdkDropListConnectedTo]="[list-1]"
or[cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
|
show 1 more comment
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.[cdkDropListConnectedTo]="[list-1]"
or[cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
Thank you. This is coming fine for single drag block, if I want multiple like [cdkDropListConnectedTo]="[list-2,list-3,list-4] is not working. how to achive that
– Jomy Joseph
Nov 22 '18 at 10:02
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
@JomyJoseph I've updated the answer to include support for linking between multiple lists. If this solves your issue, please accept the answer. If not, let us know.
– GCSDC
Nov 22 '18 at 15:12
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
Sure. You are rock. I missed single quote for each id.
– Jomy Joseph
Nov 22 '18 at 15:58
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.
[cdkDropListConnectedTo]="[list-1]"
or [cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
in angular 7.2.1 i had to use around cdkDropListConnectedTo to work even with one list.
[cdkDropListConnectedTo]="[list-1]"
or [cdkDropListConnectedTo]="['list-1']"
– Dirk
Jan 15 at 14:04
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:
cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
@Dirk tested it with angular 7.2.0 and angular material/cdk 7.2.1 and it worked using the same syntax as before:
cdkDropListConnectedTo="list-1"
– GCSDC
Jan 15 at 20:39
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413175%2fhow-to-use-angular7-angular-material-drag-drop-between-two-components%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown