【发布时间】:2022-02-16 16:54:09
【问题描述】:
将 Angular 与 Angular Material 结合使用,我创建了一个具有拖放列表的示例应用程序,其中一个使用虚拟滚动。但是,对于某些项目,drop 事件的previousIndex 值会突然跳转到不同的值。我很困惑。
复制步骤
将数字 1 到 10 从“待办事项”列表拖到“完成”列表中。当包含“10”的卡片被丢弃时,会转移错误的卡片,并且启用该操作的previousIndex 会从控制台日志中看到的从 0 跳转到 1。
代码
Stackblitz 是 here。
HTML
<div class="whole_thing" cdkDropListGroup>
<div class="example-container">
<h2>To do (CDK virtual scroll)</h2>
<div
cdkDropList
[cdkDropListData]="todo"
class="example-list"
(cdkDropListDropped)="drop($event)">
<cdk-virtual-scroll-viewport itemSize="62" class="example-viewport">
<div class="example-box" *cdkVirtualFor="let item of todo" cdkDrag>{{item}}</div>
</cdk-virtual-scroll-viewport>
</div>
</div>
<div class="example-container">
<h2>Done (regular list)</h2>
<div
cdkDropList
[cdkDropListData]="done"
class="example-list"
(cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let item of done" cdkDrag>{{item}}</div>
</div>
</div>
打字稿
import { Component, OnInit } from '@angular/core';
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';
@Component({
selector: 'app-thing',
templateUrl: './thing.component.html',
styleUrls: ['./thing.component.css']
})
export class ThingComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
todo = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16'
];
done = [
'Get up',
'Brush teeth',
'Take a shower',
'Check e-mail',
'Walk dog'
];
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);
}
//spread operator forces reload
this.todo = [...this.todo]
this.done = [...this.done]
console.log(this.done)
console.log(event)
}
}
CSS
.example-container {
width: 400px;
max-width: 100%;
margin: 0 25px 25px 0;
display: inline-block;
vertical-align: top;
}
.example-list {
border: solid 1px #ccc;
min-height: 60px;
background: white;
border-radius: 4px;
overflow: hidden;
display: block;
}
.example-box {
height: 61px;
/* padding: 20px 10px; */
border-bottom: solid 1px rgb(192, 11, 11);
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-placeholder {
opacity: 0;
}
.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-viewport {
height: 244px;
width: 400px;
}
.example-item {
height: 50px;
}
.whole_thing {
width: 3000px;
}
.bigscroller {
height: 600px;
width: 100%;
}
【问题讨论】:
-
我认为
<cdk-virtual-scroll-viewport有一些奇怪的地方,因为当我拖放“10”时,它会添加一些其他元素。尝试使用更简单的替代方法