1
 this.formgroup = formbuilder.group({
      ordering_data: formgroup.array([this.createOrder()]),
    });

  createOrder(): FormGroup {
    return this.fb.group({
      order: [null, [Validators.required]],
      section_menu_id: [null, [Validators.required]],
    });
  }

Api Result

[
  {
    "order": 0,
    "section_menu_id": 3
  },
  {
    "order": 1,
    "section_menu_id": 1
  },
  {
    "order": 2,
    "section_menu_id": 6
  },
]

I try to show all those data from api result on my form input. Can someone tell me how to patch the api result into formgroup. Thanks in advance

2
  • When did you get your data ? You need to initializate the form with order and section_menu_id. I don't see where you do this Commented Oct 12, 2020 at 9:48
  • i got the data on ngOnInit @Emilien Commented Oct 12, 2020 at 9:56

3 Answers 3

1

In my projects, I do something like this :

const data = [
  {
    "order": 0,
    "section_menu_id": 3
  },
  {
    "order": 1,
    "section_menu_id": 1
  },
  {
    "order": 2,
    "section_menu_id": 6
  },
];

constructor(private fb: FormBuilder) {}

ngOnInit() {
  this.formgroup = this.fb.group({
    ordering_data: this.fb.array(this.data.map(item => this.createOrder(item))),
  });
}

createOrder(item?: any): FormGroup {
  return this.fb.group({
    order: [item ? item.order : null, Validators.required],
    section_menu_id: [item ? item.section_menu_id : null, Validators.required],
  });
}
Sign up to request clarification or add additional context in comments.

4 Comments

its worked for me , but idont know why first of my value of my array object ([0]) kinda weird.. order: null, section_menu_id: 3. the rest of them is right
@Benni I've edited my post, please check the createOrder function
yeah now it fully work, thank you ;D . Anw it got some typo on ur code sir section_menu_id: [item ? item.section_menu_id || null, Validators.required] , it should be section_menu_id: [item ? item.section_menu_id : null, Validators.required]
@Benni Super ! Yeah god I've edited my post again :P Thanks for feedback.
0

Have you tried the following?

this.formGroup.controls.ordering_data.patchValue(apiResult);

Cannot test it locally. Maybe you could provide a stackblitz?

1 Comment

yeah i have tried that, but the result isnt as expected. this is the resulted when i tried with that code { "ordering_data": [ { "order": 0, "section_menu_id": 3 } ] }
0

consider below but I didn't test it;

interface Order {
  order: number;
  section_menu_id: number;
}

ngOnInit() {
  this.formgroup = formbuilder.group({
     ordering_data: formgroup.array([]),
  });
}

createOrder(o: Order): FormGroup {
  return this.fb.group({
     order: [o.order, [Validators.required]],
     section_menu_id: [o.section_menu_id, [Validators.required]],
  });
}

getOrders() {
   this.http.get(url).subscribe(ordersResponse: Order[]) => {
     const ordering_data = this.formgroup.get('ordering_data') as FormArray;
     ordersResponse.forEach((o: Order) => {
        ordering_data.push(this.createOrder(o.order, o.section_menu_id));
     }

     this.formgroup.updateValueAnDValidity();
   });
}

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.