1

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

James Z
  • 12,209
  • 10
  • 24
  • 44
Flavio
  • 123
  • 2
  • 12

1 Answers1

3

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

Flavio
  • 123
  • 2
  • 12
  • 1
    Try this to filter you final data source [filter](https://thewebdev.info/2021/02/23/how-to-remove-duplicates-from-an-array-of-objects-in-javascript/#:~:text=const%20result%20%3D%20arr.filter((thing%2C%20index%2C%20self)%20%3D%3E%0A%20%20index%20%3D%3D%3D%20self.findIndex((t)%20%3D%3E%20(%0A%20%20%20%20JSON.stringify(t)%20%3D%3D%3D%20JSON.stringify(thing)%0A%20%20))%0A)%0Aconsole.log(result)) – Mr. Learner Jul 02 '22 at 07:06
  • 1
    really not (if you drop out you need remove not add). Use a FormControl and use `this.myTestForm.get('myDivName').setValue(event.container.data)`. Yes a FormControl can store an array of object. – Eliseo Jul 02 '22 at 11:13