You need to enable JavaScript to run this app. 使用Angular Material实现拖放列表时CDK虚拟滚动列表的previousIndex异常跳变如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

使用Angular Material实现拖放列表时CDK虚拟滚动列表的previousIndex异常跳变如何解决?

Angular 结合CDK虚拟滚动的拖放列表索引异常问题

问题描述

基于Angular + Angular Material实现跨列表拖放功能,其中“To do”列表接入CDK虚拟滚动逻辑,“Done”列表为普通渲染列表。实际操作时,drop事件返回的previousIndex值会出现无预期跳变,最终导致实际传输的列表项与用户目标拖拽项不匹配,当前尚未定位到问题根因。

复现步骤

  • 将“To do”列表中的数字1至10,逐个拖拽到“Done”列表中
  • 当放置内容为“10”的卡片时,会触发传输错误卡片的异常
  • 查看控制台日志可发现,触发该次操作的previousIndex值从0跳变为1,与实际拖拽项在原列表的索引完全不符

异常表现可通过动态演示直观观察:拖拽序号为10的列表项完成跨列表放置时,实际被移动的列表项并非目标操作的“10”,事件返回的索引值与元素实际位置不对应。

示例代码

页面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>
</div>

组件TypeScript逻辑

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
      );
    }
    // 通过展开运算符强制触发视图刷新
    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;
  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%;
}

内容的提问来源于stack exchange,提问作者Alex Heebs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.11 21:54:08