0

I have an Array of Student Objects. The interface looks something like this:

interface Student {
  title: string;
  firstName: string;
  lastName: string;
  dob: string;
  age: number;
}

I want to edit title, firstName & lastName.

The form will have array of student objects. There is already some data in objects fetched from db. title is a dropdown, firstName and lastName are textboxes.

There will be a save button, on click of which the values of the form should be bundled and sent through Student[] to typescript.

How can I achieve this?

5
  • please check this sample , it may help you : stackoverflow.com/questions/53337587/… Commented Nov 1, 2019 at 7:20
  • The similar method can be used in your case , you just need to load the already existing existing data to form Commented Nov 1, 2019 at 7:22
  • 1
    also try to post your code here , like what you have tried so far Commented Nov 1, 2019 at 7:23
  • title will be displayed as dropdown.. how to handle it? Commented Nov 1, 2019 at 7:24
  • I read the question three times, but I am still not sure what you are asking. Commented Nov 1, 2019 at 8:07

2 Answers 2

3

You could use a Reactive Form for this.

First get your data and generate a FormGroup accordingly. I'm doing that using FormBuilder in the ngOnInit method.

Whatever you get from the API can be mapped as a FormArray of FormGroup(s).

Now in the template, you would just have to use the formControlName directive on a select list to auto-populate it with the API data for the title property.

Give this a try:

import { Component } from "@angular/core";
import { FormGroup, FormArray, FormBuilder, FormControl } from "@angular/forms";
import { HttpClient } from "@angular/common/http";

interface Student {
  title: string;
  firstName: string;
  lastName: string;
  dob: string;
  age: number;
}

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular";
  form: FormGroup;
  constructor(private http: HttpClient, private fb: FormBuilder) {}

  ngOnInit() {
    this.http.get("/assets/data.json").subscribe((students: Array<Student>) => {
      this.form = this.fb.group({
        students: this.fb.array(
          students.map(student =>
            this.fb.group({
              title: [student.title],
              firstName: [student.firstName],
              lastName: [student.lastName],
              dob: [student.dob],
              age: [student.age]
            })
          )
        )
      });
    });
  }

  onSubmit() {
    console.log("Form Value: ", this.form.value);
  }
}

And in the template:

<form 
  *ngIf="form" 
  [formGroup]="form" 
  (submit)="onSubmit()">
  <div 
    formArrayName="students" 
    *ngFor="let studentFormGroup of form.controls['students'].controls; let i = index;">
    <div [formGroupName]="i">
      <label for="title">Title</label>
      <select name="title" id="title" formControlName="title">
        <option value="" disabled>Select</option>
        <option value="Mr.">Mr.</option>
        <option value="Mrs.">Mrs.</option>
        <option value="Ms.">Ms.</option>
      </select>
      <br>
      <label for="firstName">First Name</label>
      <input type="text" id="firstName" formControlName="firstName">
      <br>
      <label for="lastName">Last Name</label>
      <input type="text" id="lastName" formControlName="lastName">
      <br>
      <label for="dob">DOB</label>
      <input type="text" id="dob" formControlName="dob">
      <br>
      <label for="age">Age</label>
      <input type="text" id="age" formControlName="age">
      <br>
      <hr>
      <br>
    </div>
  </div>
  <button type="submit">Submit</button>
</form>

Here's a Working Code Sample for your ref.

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

Comments

0

You can use the *ngFor property on an element like <li>, <div> or <ng-container> to display each student like this:

<ul>
  <li *ngFor="let student of students">
    {{ student.firstName }}
  </li>
</ul>

Because of Angular's two way binding you can put html from input in there bound to the student's properties.

Sources:

https://angular.io/guide/displaying-data

https://angular.io/api/core/Input

https://docs.angularjs.org/api/ng/directive/select

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.