Angular 拖放 - 无法将项目拖放到目标位置
Angular Drag and Drop - can't drop items to the target location
我的 Angular 9 项目有问题,拖放功能无法始终如一地工作。每当我尝试将一个对象拖到它的目标位置时,拖放永远不会完成。大多数情况下,对象 returns 回到其原始位置,或者它移动到我要将对象移动到的目标位置之前或之后的另一个位置。这是我的代码:
<div class="col-sm-12">
<div cdkDropList class="example-list cdk-drop-list-receiving" (cdkDropListDropped)="drop($event)">
<ng-container *ngFor="let word of Words; let i = index;">
<div class="row" *ngIf="((i % 2) == 0)" style="margin-right: 0px; margin-left: 0px;" >
<div class="col-sm-1 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i }}</div>
<div class="col-sm-5 text-center example-box" style="border: 1px solid black" cdkDrag ><div class="example-custom-placeholder" *cdkDragPlaceholder></div>{{ Words[i].Title }}</div>
<div class="col-sm-1 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i+1 }}</div>
<div class="col-sm-5 text-center example-box" *ngIf="Words[i+1] !== undefined" style="border: 1px solid black" cdkDrag ><div class="example-custom-placeholder" *cdkDragPlaceholder></div>{{ Words[i+1].Title }}</div>
</div>
</ng-container>
</div>
</div>
这是应用程序的 link 直播,因此您可以直观地看到我看到的问题:[更新] https://stackblitz.com/edit/drag-and-drop-issue
提前致谢
Cdk 下拉列表垂直或水平工作。在你的代码中索引被搞砸了。你真正需要的是一个 CdkDropListGroup 让它双向运行。我已经为您的布局创建了一个代码。我已经使同一个数组在同一个 cdkDropListGroup 中表现得像两个不同的 cdkdroplist。
工作 Stackblitz :- https://stackblitz.com/edit/drag-and-drop-issue-gw4epd
HTML
<div cdkDropListGroup class="row example-list cdk-drop-list-receiving">
<div class="row">
<div class="col-6 group" cdkDropList id="listOne" (cdkDropListDropped)="drop($event)" [cdkDropListConnectedTo]="['listTwo']">
<ng-container *ngFor="let word of Words; let i=index">
<div *ngIf="(i%2)==0" class="row" style="margin-right: 0px; margin-left: 0px;">
<div class="col-sm-2 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i }}
</div>
<div class="col-sm-10 text-center example-box" style="border: 1px solid black" cdkDrag>
<div class="example-custom-placeholder col-sm-10" *cdkDragPlaceholder></div>
{{ Words[i].Title }}
</div>
</div>
</ng-container>
</div>
<div class="col-6 group" cdkDropList id="listTwo" (cdkDropListDropped)="drop($event)" [cdkDropListConnectedTo]="['listOne']">
<ng-container *ngFor="let word of Words; let i=index">
<div *ngIf="(i%2)!==0" class="row" style="margin-right: 0px; margin-left: 0px;">
<div class="col-sm-2 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i }}
</div>
<div class="col-sm-10 text-center example-box" style="border: 1px solid black" cdkDrag>
<div class="example-custom-placeholder col-sm-10" *cdkDragPlaceholder></div>
{{ Words[i].Title }}
</div>
</div>
</ng-container>
</div>
</div>
</div>
<!-- Copyright 2019 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license -->
TS :-
import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
/**
* @title Drag&Drop custom placeholder
*/
@Component({
selector: 'cdk-drag-drop-custom-placeholder-example',
templateUrl: 'cdk-drag-drop-custom-placeholder-example.html',
styleUrls: ['cdk-drag-drop-custom-placeholder-example.css'],
})
export class CdkDragDropCustomPlaceholderExample {
Words = [
{Title: 'Test A'},
{Title: 'Test B'},
{Title: 'Test C'},
{Title: 'Test D'},
{Title: 'Test E'},
{Title: 'Test F'},
{Title: 'Test G'},
{Title: 'Test H'},
{Title: 'Test I'},
{Title: 'Test J'},
{Title: 'Test K'},
{Title: 'Test L'},
{Title: 'Test M'},
{Title: 'Test N'},
{Title: 'Test O'}
];
drop(event: CdkDragDrop<string[]>) {
console.log(event);
if (event.previousContainer === event.container) {
console.log("same Container");
if(event.previousContainer.id === 'listOne'){
moveItemInArray(this.Words, event.previousIndex*2, event.currentIndex*2);
} else {
moveItemInArray(this.Words, (event.previousIndex*2)+1, (event.currentIndex*2)+1)
}
} else {
console.log("different Container");
var connectedTo = event.previousContainer.connectedTo.toString();
console.log(connectedTo)
if(connectedTo ==='listOne') {
console.log('list2');
moveItemInArray(this.Words, (event.previousIndex*2)+1, (event.currentIndex*2));
} else {
console.log('list1');
moveItemInArray(this.Words, (event.previousIndex*2), (event.currentIndex*2)+1);
}
}
}
}
/** Copyright 2019 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
CSS :-
.example-list {
width: 500px;
max-width: 100%;
border: solid 1px #ccc;
min-height: 60px;
display: block;
background: white;
border-radius: 4px;
overflow: hidden;
}
.group{
padding: 0;
margin: 0;
}
.example-box {
padding: 20px 10px;
border-bottom: solid 1px #ccc;
color: rgba(0, 0, 0, 0.87);
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
cursor: move;
background: white;
font-size: 14px;
}
.cdk-drag-preview {
box-sizing: border-box;
border-radius: 4px;
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
0 8px 10px 1px rgba(0, 0, 0, 0.14),
0 3px 14px 2px rgba(0, 0, 0, 0.12);
}
.cdk-drag-animating {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.example-box:last-child {
border: none;
}
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.example-custom-placeholder {
background: #ccc;
border: dotted 3px #999;
min-height: 60px;
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
我的 Angular 9 项目有问题,拖放功能无法始终如一地工作。每当我尝试将一个对象拖到它的目标位置时,拖放永远不会完成。大多数情况下,对象 returns 回到其原始位置,或者它移动到我要将对象移动到的目标位置之前或之后的另一个位置。这是我的代码:
<div class="col-sm-12">
<div cdkDropList class="example-list cdk-drop-list-receiving" (cdkDropListDropped)="drop($event)">
<ng-container *ngFor="let word of Words; let i = index;">
<div class="row" *ngIf="((i % 2) == 0)" style="margin-right: 0px; margin-left: 0px;" >
<div class="col-sm-1 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i }}</div>
<div class="col-sm-5 text-center example-box" style="border: 1px solid black" cdkDrag ><div class="example-custom-placeholder" *cdkDragPlaceholder></div>{{ Words[i].Title }}</div>
<div class="col-sm-1 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i+1 }}</div>
<div class="col-sm-5 text-center example-box" *ngIf="Words[i+1] !== undefined" style="border: 1px solid black" cdkDrag ><div class="example-custom-placeholder" *cdkDragPlaceholder></div>{{ Words[i+1].Title }}</div>
</div>
</ng-container>
</div>
</div>
这是应用程序的 link 直播,因此您可以直观地看到我看到的问题:[更新] https://stackblitz.com/edit/drag-and-drop-issue
提前致谢
Cdk 下拉列表垂直或水平工作。在你的代码中索引被搞砸了。你真正需要的是一个 CdkDropListGroup 让它双向运行。我已经为您的布局创建了一个代码。我已经使同一个数组在同一个 cdkDropListGroup 中表现得像两个不同的 cdkdroplist。
工作 Stackblitz :- https://stackblitz.com/edit/drag-and-drop-issue-gw4epd
HTML
<div cdkDropListGroup class="row example-list cdk-drop-list-receiving">
<div class="row">
<div class="col-6 group" cdkDropList id="listOne" (cdkDropListDropped)="drop($event)" [cdkDropListConnectedTo]="['listTwo']">
<ng-container *ngFor="let word of Words; let i=index">
<div *ngIf="(i%2)==0" class="row" style="margin-right: 0px; margin-left: 0px;">
<div class="col-sm-2 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i }}
</div>
<div class="col-sm-10 text-center example-box" style="border: 1px solid black" cdkDrag>
<div class="example-custom-placeholder col-sm-10" *cdkDragPlaceholder></div>
{{ Words[i].Title }}
</div>
</div>
</ng-container>
</div>
<div class="col-6 group" cdkDropList id="listTwo" (cdkDropListDropped)="drop($event)" [cdkDropListConnectedTo]="['listOne']">
<ng-container *ngFor="let word of Words; let i=index">
<div *ngIf="(i%2)!==0" class="row" style="margin-right: 0px; margin-left: 0px;">
<div class="col-sm-2 text-center" style="padding: 20px 10px; border: 1px solid black"> {{ i }}
</div>
<div class="col-sm-10 text-center example-box" style="border: 1px solid black" cdkDrag>
<div class="example-custom-placeholder col-sm-10" *cdkDragPlaceholder></div>
{{ Words[i].Title }}
</div>
</div>
</ng-container>
</div>
</div>
</div>
<!-- Copyright 2019 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license -->
TS :-
import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
/**
* @title Drag&Drop custom placeholder
*/
@Component({
selector: 'cdk-drag-drop-custom-placeholder-example',
templateUrl: 'cdk-drag-drop-custom-placeholder-example.html',
styleUrls: ['cdk-drag-drop-custom-placeholder-example.css'],
})
export class CdkDragDropCustomPlaceholderExample {
Words = [
{Title: 'Test A'},
{Title: 'Test B'},
{Title: 'Test C'},
{Title: 'Test D'},
{Title: 'Test E'},
{Title: 'Test F'},
{Title: 'Test G'},
{Title: 'Test H'},
{Title: 'Test I'},
{Title: 'Test J'},
{Title: 'Test K'},
{Title: 'Test L'},
{Title: 'Test M'},
{Title: 'Test N'},
{Title: 'Test O'}
];
drop(event: CdkDragDrop<string[]>) {
console.log(event);
if (event.previousContainer === event.container) {
console.log("same Container");
if(event.previousContainer.id === 'listOne'){
moveItemInArray(this.Words, event.previousIndex*2, event.currentIndex*2);
} else {
moveItemInArray(this.Words, (event.previousIndex*2)+1, (event.currentIndex*2)+1)
}
} else {
console.log("different Container");
var connectedTo = event.previousContainer.connectedTo.toString();
console.log(connectedTo)
if(connectedTo ==='listOne') {
console.log('list2');
moveItemInArray(this.Words, (event.previousIndex*2)+1, (event.currentIndex*2));
} else {
console.log('list1');
moveItemInArray(this.Words, (event.previousIndex*2), (event.currentIndex*2)+1);
}
}
}
}
/** Copyright 2019 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
CSS :-
.example-list {
width: 500px;
max-width: 100%;
border: solid 1px #ccc;
min-height: 60px;
display: block;
background: white;
border-radius: 4px;
overflow: hidden;
}
.group{
padding: 0;
margin: 0;
}
.example-box {
padding: 20px 10px;
border-bottom: solid 1px #ccc;
color: rgba(0, 0, 0, 0.87);
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
cursor: move;
background: white;
font-size: 14px;
}
.cdk-drag-preview {
box-sizing: border-box;
border-radius: 4px;
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
0 8px 10px 1px rgba(0, 0, 0, 0.14),
0 3px 14px 2px rgba(0, 0, 0, 0.12);
}
.cdk-drag-animating {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.example-box:last-child {
border: none;
}
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.example-custom-placeholder {
background: #ccc;
border: dotted 3px #999;
min-height: 60px;
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}