【问题标题】:Angular Drag and Drop with trackby is snapping back使用 trackby 的角度拖放正在快速恢复
【发布时间】:2021-04-19 04:48:51
【问题描述】:

我想使用 cdk 拖放来重新排序 ngFor 列表,但是因为我不希望在放置后为每个元素再次触发动画,所以我也想使用 trackby

问题是当我使用 trackby 时,每次拖动都会回弹,而不是保持在缩进的索引位置

<ng-container *ngIf="tabObj$ | async as tab">
        <div
            cdkDropList
            [cdkDropListData]="memberList"
            [cdkDropListConnectedTo]="bumble"
            [cdkDropListDisabled]="isDisabled"
            (cdkDropListDropped)="drop($event)"
            [id]="tabid"
            [style]="template.style">
            <div class="indication" (click)="handleInfoClick(tab)">{{ALTtabid}} -- {{tabid}}</div>
            <sss-node
                *ngFor="let pointerobj of memberList; let i = index; trackBy: identify;"
                cdkDrag
                [cdkDragData]="pointerobj"
                class="cdk-drag-animating"
                [ngClass]="pointerobj._id"
                [loading]="pointerobj.loading"
                [ancestry]="getAncestry(pointerobj, i)"
                [bubbleUp]="handleBubble.bind(this)">
            </sss-node>
        </div>
    </ng-container>


drop(event: CdkDragDrop<string[]>) {
    console.log("this.tabid", this.tabid);
    console.log("event", event);
    this.goforward(event);

    console.log("boo");
}

goforward(event:any){
  if (event.previousContainer === event.container) {
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
//   this.memberList.splice(event.currentIndex, 0, this.memberList.splice(event.previousIndex, 1)[0]);
  } else {
  //   transferArrayItem(event.previousContainer.data,
  //                    event.container.data,
  //                    event.previousIndex,event.currentIndex);
  }
}


identify = (index, pointer: Pointer) => { return pointer.instance; }

【问题讨论】:

  • 你能创建一个 stackblits 项目以便我们进行工作吗?

标签: angular drag-and-drop


【解决方案1】:

pointer.instance 仍然是一个对象,所以trackBy 不知道它被改变了。

试试这个:

identify = (index, pointer: Pointer) => { return pointer.instance.id; }

或您的实例上的任何其他 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多