gpt4 book ai didi

具有动态列表的数组之间的 Angular7 cdkDropList

转载 作者:行者123 更新时间:2023-12-02 03:26:41 25 4
gpt4 key购买 nike

我收到以下错误:错误 TypeError:无法分配给字符串“One”的只读属性“2”来 self 创建的用于模仿实际数据的形式的以下数据列表,以及我从 Angular Material 文档中看到的用于此类动态列表的模板 View 。但即使他们说了要做什么,他们也没有给出例子..

我无法执行以下操作:

组件:

lists = [];

ngOnInit() {
this.lists = [{number: 'One', amount: [1, 2, 3, 4]}, {number: 'Two', amount: [5, 6, 7, 8]}, {number: 'Three', amount: [9, 10, 11, 12]}];
}

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);
}
}

模板:

<div cdkDropList [cdkDropListData]="list.number" *ngFor="let list of lists" (cdkDropListDropped)="drop($event)">
{{list.number}}
<div cdkDrag [cdkDragData]="item" *ngFor="let item of list.amount">
{{item}}
</div>
</div>

最佳答案

您应该获得列表项的实际索引。试试这个:

TS:

activeNumIndex: number;
enter(i) {
this.activeNumIndex = i;
}

drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(this.lists[this.activeNumIndex].amount, event.previousIndex, event.currentIndex);
} else {
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}
}

HTML:

<div cdkDropList [cdkDropListData]="list.number" *ngFor="let list of lists; let i=index;" (cdkDropListDropped)="drop($event)">
{{list.number}}
<div cdkDrag [cdkDragData]="item" *ngFor="let item of list.amount" (mouseenter)="enter(i)" style="border:1px solid black">
{{item}}
</div>
</div>

Here's a duplicate in Stackblitz

关于具有动态列表的数组之间的 Angular7 cdkDropList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53190586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com