1

I have constructed my form like this.

this.myForm = this.fBuilder.group({
  openingHoursForm: this.fBuilder.array([])
});

this.openingHoursArray = data.body.OpeningHours;

this.openingHoursArray.forEach(element => {

(<FormArray>this.myForm.get('openingHoursForm')).push(this.fBuilder.group({
                            Id: [element.Id],
                            Heading: [element.Heading],
                            Subheading: [element.Subheading],
                            When: [element.When],
                            Times: [element.Times],
                            Emphasis: [element.Emphasis],
                            SortOrder: [element.SortOrder]
                    }));
                });

But how do I patch the value of "SortOrder" at a specific index. I have this but it's not working:

moveRowUp(currentIndex,fixture)
    {
        const control = <FormArray>this.myForm.controls['openingHoursForm'];
        control.at(currentIndex)['SortOrder'].patchValue(currentIndex + 1);
        console.log(this.myForm);
    }

2 Answers 2

1

try this

 moveRowUp(currentIndex,fixture)
  {
      const control = this.myForm.get(['openingHoursForm',currentIndex,'SortOrder']) as FormControl;
      control.patchValue(currentIndex + 1);

  }
Sign up to request clarification or add additional context in comments.

4 Comments

No that didn't work unfortunately. Your code doesn't mention amending a record at a particular index level in the array though. Thanks for trying anway :)
this.myForm.get(['openingHoursForm',currentIndex,'SortOrder']) will get the sortorder at particular index if currentindex holds the index value
Ah ok, I put it in, it didn't work but it didn't error either.
console.log(control) and see what its returning
0

There's probably an easier of doing it, but in the end I did it like this:

moveRowUp(fixture)
    {
        var newVal = fixture.controls['SortOrder'].value - 1;
        fixture.controls['SortOrder'].patchValue(newVal);
        console.log(record);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.