Rachit
Rachit

Reputation: 79

Facing issue with binding Data from JSON Object to <mat-select>

I am having trouble in populating the drop-down list from the JSON Response object coming from the API..

  1. component.ts code

    for (let k in keys) {
            this.form.controls['id'].setValue(data[k].name);
            console.log(data[k].name);
        }
    });       
    
  2. component.html code

    <mat-form-field>
        <mat-label>Select a User</mat-label>
        <mat-select formControlName="id">
            <mat-option *ngFor="let opt of options" [value]="opt.data" >
                {{ opt.data.name}}
            </mat-option>
        </mat-select>
    </mat-form-field>
    
  3. console.log(data)

    0: {id: 1, name: "User1"}
    1: {id: 2, name: "User2"}
    2: {id: 3, name: "User3"}
    3: {id: 4, name: "User4"}
    ...
    
  4. console.log(data[k].name); //This is the data I need on drop-down

      User1
      User2
      User3
      User4
      ...
    

The console.log data shows the index on every object. My JSON Object is pretty simple though.
It looks like:

[
 {
    "id": 1,
    "name": "User1"
 },
 {
    "id": 2,
    "name": "User2"
 },...
]

Kindly let me know what am I doing wrong. Thanks.

 EDIT
Here's working Stackblitz Example

Upvotes: 0

Views: 1804

Answers (2)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

Try this:

<mat-form-field>
    <mat-label>Select a User</mat-label>
    <mat-select formControlName="id">
            <mat-option *ngFor="let item of list" [value]="item" >
                {{ item.name }}
            </mat-option>
        </mat-select>
</mat-form-field>

Modify your TS file according to this :

list:any=[];
 getUsers(): void {
        this.usersService.getUsers()
        .subscribe(users => {

            this.users = users;
            const data: User[] = this.users;
            console.log('data',data)
            console.log("object",Object.keys(data));
            const keys: number[] = Object.keys(data) as any;
            console.log("keys",keys);
            console.log("users",this.users);
            for (let k in keys) {
                this.form.controls['id'].setValue(data[k].name);
                console.log(data[k]);
                this.list.push(data[k]);
            }
        });
    }

Upvotes: 0

Prashant Pimpale
Prashant Pimpale

Reputation: 10697

If I understand the question correctly, your API response is a list so just bind that with a for loop to Mat-Option.

HTML Code:

<mat-card>
    <form [formGroup]="form" (submit)="add()">
        <mat-form-field>
            <mat-label>Select a User</mat-label>
            <mat-select formControlName="id">
                                               \/\/
                <mat-option *ngFor="let key of users" [value]="key">
                    {{ key.name }}
                </mat-option>
            </mat-select>
        </mat-form-field>
        <br/>
            <div fxLayout>
                <div>
                    <button
                    mat-raised-button
                    color="accent" [disabled] = "form.invalid">Save
                </button>
            </div>
        </div>
    </form>
</mat-card>

TS Code:

getUsers(): void {
  this.usersService.getUsers().subscribe(users => {
        this.users = users;
        console.log(this.users)
  });
}

Forked_Stackblitz

Upvotes: 2

Related Questions