Angular 7 Drag and Drop - Dynamically Create Drop Zones
up vote
3
down vote
favorite
Is there a way to dynamically create drop zones? I'm having some troubles with ngFor and cdkDropList.
Here is my first list and draggable elements:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="[lessonList]"
(cdkDropListDropped)="drop($event)"
>
<div class="subject" *ngFor="let subject of subjects" cdkDrag>
{{subject.name}}
</div>
</div>
And here is my second list:
<div class="conta" cdkDropList
#lessonList="cdkDropList"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)">
<div class="sub" cdkDrag *ngFor="let lesson of appointment.lessons">
{{lesson.name}}
</div>
</div>
Now, div with class 'conta' is inside of a *ngFor.
My problem is, I suppose, with my second list. If I drag an element from second list to list one, it works normally, but if I try to drag element from list one to any instance of list in second list, it can't recognize that the element is being dragged. Demo here:
Am I doing something wrong here?
The typescript part is working fine.
Thanks
drag-and-drop angular7 angular-cdk
add a comment |
up vote
3
down vote
favorite
Is there a way to dynamically create drop zones? I'm having some troubles with ngFor and cdkDropList.
Here is my first list and draggable elements:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="[lessonList]"
(cdkDropListDropped)="drop($event)"
>
<div class="subject" *ngFor="let subject of subjects" cdkDrag>
{{subject.name}}
</div>
</div>
And here is my second list:
<div class="conta" cdkDropList
#lessonList="cdkDropList"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)">
<div class="sub" cdkDrag *ngFor="let lesson of appointment.lessons">
{{lesson.name}}
</div>
</div>
Now, div with class 'conta' is inside of a *ngFor.
My problem is, I suppose, with my second list. If I drag an element from second list to list one, it works normally, but if I try to drag element from list one to any instance of list in second list, it can't recognize that the element is being dragged. Demo here:
Am I doing something wrong here?
The typescript part is working fine.
Thanks
drag-and-drop angular7 angular-cdk
Does your second list work if you remove the styling that makes it multiline? The droplists can only be either horizontal or vertical. What you seem to have here is a grid, which won't work because there is inherent logic that relies on knowing whether to calculate the relative distances of droplist elements in x or y dimension.
– Lightheaded
2 days ago
@Lightheaded - Yes, I actually thought of that, and removed all styles, but no. I found a solution. There was a problem with cdkDropListConnectedTo. It was connected to null, so I made my workaround. Check my answer bellow, and thanks!
– sebamed
2 days ago
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Is there a way to dynamically create drop zones? I'm having some troubles with ngFor and cdkDropList.
Here is my first list and draggable elements:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="[lessonList]"
(cdkDropListDropped)="drop($event)"
>
<div class="subject" *ngFor="let subject of subjects" cdkDrag>
{{subject.name}}
</div>
</div>
And here is my second list:
<div class="conta" cdkDropList
#lessonList="cdkDropList"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)">
<div class="sub" cdkDrag *ngFor="let lesson of appointment.lessons">
{{lesson.name}}
</div>
</div>
Now, div with class 'conta' is inside of a *ngFor.
My problem is, I suppose, with my second list. If I drag an element from second list to list one, it works normally, but if I try to drag element from list one to any instance of list in second list, it can't recognize that the element is being dragged. Demo here:
Am I doing something wrong here?
The typescript part is working fine.
Thanks
drag-and-drop angular7 angular-cdk
Is there a way to dynamically create drop zones? I'm having some troubles with ngFor and cdkDropList.
Here is my first list and draggable elements:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="[lessonList]"
(cdkDropListDropped)="drop($event)"
>
<div class="subject" *ngFor="let subject of subjects" cdkDrag>
{{subject.name}}
</div>
</div>
And here is my second list:
<div class="conta" cdkDropList
#lessonList="cdkDropList"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)">
<div class="sub" cdkDrag *ngFor="let lesson of appointment.lessons">
{{lesson.name}}
</div>
</div>
Now, div with class 'conta' is inside of a *ngFor.
My problem is, I suppose, with my second list. If I drag an element from second list to list one, it works normally, but if I try to drag element from list one to any instance of list in second list, it can't recognize that the element is being dragged. Demo here:
Am I doing something wrong here?
The typescript part is working fine.
Thanks
drag-and-drop angular7 angular-cdk
drag-and-drop angular7 angular-cdk
asked Nov 10 at 12:45
sebamed
4918
4918
Does your second list work if you remove the styling that makes it multiline? The droplists can only be either horizontal or vertical. What you seem to have here is a grid, which won't work because there is inherent logic that relies on knowing whether to calculate the relative distances of droplist elements in x or y dimension.
– Lightheaded
2 days ago
@Lightheaded - Yes, I actually thought of that, and removed all styles, but no. I found a solution. There was a problem with cdkDropListConnectedTo. It was connected to null, so I made my workaround. Check my answer bellow, and thanks!
– sebamed
2 days ago
add a comment |
Does your second list work if you remove the styling that makes it multiline? The droplists can only be either horizontal or vertical. What you seem to have here is a grid, which won't work because there is inherent logic that relies on knowing whether to calculate the relative distances of droplist elements in x or y dimension.
– Lightheaded
2 days ago
@Lightheaded - Yes, I actually thought of that, and removed all styles, but no. I found a solution. There was a problem with cdkDropListConnectedTo. It was connected to null, so I made my workaround. Check my answer bellow, and thanks!
– sebamed
2 days ago
Does your second list work if you remove the styling that makes it multiline? The droplists can only be either horizontal or vertical. What you seem to have here is a grid, which won't work because there is inherent logic that relies on knowing whether to calculate the relative distances of droplist elements in x or y dimension.
– Lightheaded
2 days ago
Does your second list work if you remove the styling that makes it multiline? The droplists can only be either horizontal or vertical. What you seem to have here is a grid, which won't work because there is inherent logic that relies on knowing whether to calculate the relative distances of droplist elements in x or y dimension.
– Lightheaded
2 days ago
@Lightheaded - Yes, I actually thought of that, and removed all styles, but no. I found a solution. There was a problem with cdkDropListConnectedTo. It was connected to null, so I made my workaround. Check my answer bellow, and thanks!
– sebamed
2 days ago
@Lightheaded - Yes, I actually thought of that, and removed all styles, but no. I found a solution. There was a problem with cdkDropListConnectedTo. It was connected to null, so I made my workaround. Check my answer bellow, and thanks!
– sebamed
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
After a full day of research, I found this pull request on Angular CDK repository on Github. Now, since I did not know how to integrate cdkDropListGroup into my example, I decited to create an array of IDs which will be added to [cdkDropListConnectedTo].
Each instance of my second list will have generated ID, and that ID will be added to array with suitable prefix (in my second list, on cdkDropList):
<div cdkDropList
[attr.id]="addId(i, j)"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)"
>
addId method:
addId(i, j) {
this.LIST_IDS.push('cdk-drop-list-' + i + '' + j);
return i + '' + j;
}
(cdk-drop-list- is an ID prefix. CDK places this prefix on every element with cdkDropList attribute)
So, my array will look like:
- cdk-drop-list-00
- cdk-drop-list-01
- cdk-drop-list-02
- etc.
Now, I pass that array to [cdkDropListConnectedTo] in my first list:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="LIST_IDS"
(cdkDropListDropped)="drop($event)"
>
And it works flawlessly!
Hope this will help anybody with the same problem. Also, take a look at the pull request I mentioned, my solution is only a workaround, there is probably a better solution with cdkDropListGroup
1
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)
– Lightheaded
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
After a full day of research, I found this pull request on Angular CDK repository on Github. Now, since I did not know how to integrate cdkDropListGroup into my example, I decited to create an array of IDs which will be added to [cdkDropListConnectedTo].
Each instance of my second list will have generated ID, and that ID will be added to array with suitable prefix (in my second list, on cdkDropList):
<div cdkDropList
[attr.id]="addId(i, j)"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)"
>
addId method:
addId(i, j) {
this.LIST_IDS.push('cdk-drop-list-' + i + '' + j);
return i + '' + j;
}
(cdk-drop-list- is an ID prefix. CDK places this prefix on every element with cdkDropList attribute)
So, my array will look like:
- cdk-drop-list-00
- cdk-drop-list-01
- cdk-drop-list-02
- etc.
Now, I pass that array to [cdkDropListConnectedTo] in my first list:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="LIST_IDS"
(cdkDropListDropped)="drop($event)"
>
And it works flawlessly!
Hope this will help anybody with the same problem. Also, take a look at the pull request I mentioned, my solution is only a workaround, there is probably a better solution with cdkDropListGroup
1
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)
– Lightheaded
2 days ago
add a comment |
up vote
0
down vote
accepted
After a full day of research, I found this pull request on Angular CDK repository on Github. Now, since I did not know how to integrate cdkDropListGroup into my example, I decited to create an array of IDs which will be added to [cdkDropListConnectedTo].
Each instance of my second list will have generated ID, and that ID will be added to array with suitable prefix (in my second list, on cdkDropList):
<div cdkDropList
[attr.id]="addId(i, j)"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)"
>
addId method:
addId(i, j) {
this.LIST_IDS.push('cdk-drop-list-' + i + '' + j);
return i + '' + j;
}
(cdk-drop-list- is an ID prefix. CDK places this prefix on every element with cdkDropList attribute)
So, my array will look like:
- cdk-drop-list-00
- cdk-drop-list-01
- cdk-drop-list-02
- etc.
Now, I pass that array to [cdkDropListConnectedTo] in my first list:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="LIST_IDS"
(cdkDropListDropped)="drop($event)"
>
And it works flawlessly!
Hope this will help anybody with the same problem. Also, take a look at the pull request I mentioned, my solution is only a workaround, there is probably a better solution with cdkDropListGroup
1
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)
– Lightheaded
2 days ago
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
After a full day of research, I found this pull request on Angular CDK repository on Github. Now, since I did not know how to integrate cdkDropListGroup into my example, I decited to create an array of IDs which will be added to [cdkDropListConnectedTo].
Each instance of my second list will have generated ID, and that ID will be added to array with suitable prefix (in my second list, on cdkDropList):
<div cdkDropList
[attr.id]="addId(i, j)"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)"
>
addId method:
addId(i, j) {
this.LIST_IDS.push('cdk-drop-list-' + i + '' + j);
return i + '' + j;
}
(cdk-drop-list- is an ID prefix. CDK places this prefix on every element with cdkDropList attribute)
So, my array will look like:
- cdk-drop-list-00
- cdk-drop-list-01
- cdk-drop-list-02
- etc.
Now, I pass that array to [cdkDropListConnectedTo] in my first list:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="LIST_IDS"
(cdkDropListDropped)="drop($event)"
>
And it works flawlessly!
Hope this will help anybody with the same problem. Also, take a look at the pull request I mentioned, my solution is only a workaround, there is probably a better solution with cdkDropListGroup
After a full day of research, I found this pull request on Angular CDK repository on Github. Now, since I did not know how to integrate cdkDropListGroup into my example, I decited to create an array of IDs which will be added to [cdkDropListConnectedTo].
Each instance of my second list will have generated ID, and that ID will be added to array with suitable prefix (in my second list, on cdkDropList):
<div cdkDropList
[attr.id]="addId(i, j)"
[cdkDropListData]="appointment.lessons"
[cdkDropListConnectedTo]="[subjectList]"
(cdkDropListDropped)="drop($event)"
>
addId method:
addId(i, j) {
this.LIST_IDS.push('cdk-drop-list-' + i + '' + j);
return i + '' + j;
}
(cdk-drop-list- is an ID prefix. CDK places this prefix on every element with cdkDropList attribute)
So, my array will look like:
- cdk-drop-list-00
- cdk-drop-list-01
- cdk-drop-list-02
- etc.
Now, I pass that array to [cdkDropListConnectedTo] in my first list:
<div class="subj-container"
cdkDropListOrientation="horizontal"
cdkDropList
#subjectList="cdkDropList"
[cdkDropListData]="subjects"
[cdkDropListConnectedTo]="LIST_IDS"
(cdkDropListDropped)="drop($event)"
>
And it works flawlessly!
Hope this will help anybody with the same problem. Also, take a look at the pull request I mentioned, my solution is only a workaround, there is probably a better solution with cdkDropListGroup
answered 2 days ago
sebamed
4918
4918
1
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)
– Lightheaded
2 days ago
add a comment |
1
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)
– Lightheaded
2 days ago
1
1
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)– Lightheaded
2 days ago
cdkDropListGroup
is not a released feature yet. Look out for next releases. Meanwhile, I've been using the same approach – use a mapping by list IDs. There seems no better way to do it at the moment. Whenever your mentioned feature gets released, you can remove all that hacky messing with IDs :)– Lightheaded
2 days ago
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239092%2fangular-7-drag-and-drop-dynamically-create-drop-zones%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
Does your second list work if you remove the styling that makes it multiline? The droplists can only be either horizontal or vertical. What you seem to have here is a grid, which won't work because there is inherent logic that relies on knowing whether to calculate the relative distances of droplist elements in x or y dimension.
– Lightheaded
2 days ago
@Lightheaded - Yes, I actually thought of that, and removed all styles, but no. I found a solution. There was a problem with cdkDropListConnectedTo. It was connected to null, so I made my workaround. Check my answer bellow, and thanks!
– sebamed
2 days ago