反应性形式的工作很奇怪,呈斜滴cdk-重复的项目

发布于 2025-02-12 12:36:54 字数 1623 浏览 0 评论 0原文

我有一个表格,用户可以将选定的IMG拖动并将其放在目标位置并将表格提交给后端。

源数据是对象(API)的数组,

问题:< / strong>

  1. 将一项拖动并将其放入拖动位置时,该项目将被乘以 /重复 - ((阵列重复的对象)
  2. 时)拖动项目移动回到其原始位置,再次重复(阵列中的对象重复)
  3. 时,当点击提交表单时,它不仅显示了很多重复的项目

期望:

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

stackblitz-问题复制

由于我的方案有些不同,我找不到任何解决方案从中

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:

  1. when drag one item and put it in drag location, the item gets multiplied / duplicated - (objects in array duplicated)
  2. when dragged item moved back to its original position, again it gets duplicated (objects in array duplicated)
  3. 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);
}

stackblitz - issue reproduced

Since my scenarios is bit different, i couldn't find any solutions from SO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果 2025-02-19 12:36:54

最后,我自己修改了以下几乎没有修改,我自己解决了问题。

初始化formControlname喜欢myDivName:this.fb.Array([]),

 get myDivName() {
    return <FormArray>this.myTestForm.get('myDivName');
  }

ins drop> drop()方法>分配并传递formValue

const formArry = this.myDivName;
formArry.insert(0,this.fb.control(event.container.data));

工作演示

注意:我不确定修复的效率有多高,但会尽可能地优化它

Finally, I fixed my issue by myself, by doing little modification like below.

initialize formControlName like myDivName: this.fb.array([]),

 get myDivName() {
    return <FormArray>this.myTestForm.get('myDivName');
  }

inside drop() method assign and pass formValue

const formArry = this.myDivName;
formArry.insert(0,this.fb.control(event.container.data));

working 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文