反应性形式的工作很奇怪,呈斜滴cdk-重复的项目
我有一个表格,用户可以将选定的IMG拖动并将其放在目标位置并将表格提交给后端。
源数据是对象(API)的数组,
问题:< / strong>
- 将一项拖动并将其放入拖动位置时,该项目将被乘以 /重复 - ((阵列重复的对象)
- 时)拖动项目移动回到其原始位置,再次重复(阵列中的对象重复)
- 时,当点击提交表单时,它不仅显示了很多重复的项目
期望:
drag and drog oflag drag ofla并提交表格。
ngOnInit() {
this.initTestForm();
}
ngAfterViewInit() { }
initTestForm() {
this.myTestForm = this.fb.group({
// myDivName: new FormControl(this.containerData),
myDivName: new FormControl(this.containerData),
});
}
drop(event: CdkDragDrop<Item[]>) {
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
);
console.log(
'transferArrayItem',
event.container.data[event.currentIndex]
);
this.addItem(event.container.data[event.currentIndex]);
}
}
get myDivName() {
return this.myTestForm.get('myDivName') as FormArray;
}
addItem(item: any) {
this.basket.push(item);
this.myDivName.push(this.fb.control(item));
}
removeItem() {
this.basket.pop();
this.myDivName.removeAt(this.myDivName.length - 1);
}
savedata() {
console.log(this.myTestForm);
}
由于我的方案有些不同,我找不到任何解决方案从中
I've a form where user can drag selected img and drop it in target location and submit the form to backend.
Source data is Array of Object(API),
Issue:
- when drag one item and put it in drag location, the item gets multiplied / duplicated - (objects in array duplicated)
- when dragged item moved back to its original position, again it gets duplicated (objects in array duplicated)
- when when hit the submit form, its not only showing lot of duplicated items
Expectation:
drag and drop only selected items and submit the form.
ngOnInit() {
this.initTestForm();
}
ngAfterViewInit() { }
initTestForm() {
this.myTestForm = this.fb.group({
// myDivName: new FormControl(this.containerData),
myDivName: new FormControl(this.containerData),
});
}
drop(event: CdkDragDrop<Item[]>) {
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
);
console.log(
'transferArrayItem',
event.container.data[event.currentIndex]
);
this.addItem(event.container.data[event.currentIndex]);
}
}
get myDivName() {
return this.myTestForm.get('myDivName') as FormArray;
}
addItem(item: any) {
this.basket.push(item);
this.myDivName.push(this.fb.control(item));
}
removeItem() {
this.basket.pop();
this.myDivName.removeAt(this.myDivName.length - 1);
}
savedata() {
console.log(this.myTestForm);
}
Since my scenarios is bit different, i couldn't find any solutions from SO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我自己修改了以下几乎没有修改,我自己解决了问题。
初始化
formControlname
喜欢myDivName:this.fb.Array([]),
ins drop> drop()方法>分配并传递formValue
工作演示
注意:我不确定修复的效率有多高,但会尽可能地优化它
。
Finally, I fixed my issue by myself, by doing little modification like below.
initialize
formControlName
likemyDivName: this.fb.array([]),
inside
drop()
method assign and pass formValueworking demo
Note: I'm not sure how efficient the fix is, but will optimize it as much as can.
Thanks to all who atleast view my question