2

I want to access array inside objects which is coming from my json file to populate my dropdown values and want to show values on second dropdown only from previous selected dropdown.

here is my ts:

 companySites = false;
  siteSelected = false;
  alldata: any = [];
getCustomerData() {
    this.maintenanceServices.getAllCustomers().subscribe(
      data => {
        if (data) {
          this.alldata = data;
          console.log(this.alldata);
        }
      },
      error => {
        console.log(error);
      }
    );
  }

  onCustomerChange(): void {
    this.companySites = true;
  }

  onSitecChange(): void {
    this.siteSelected = true;
  }

.html:

<mat-grid-tile>
      <div>
        <p>Select a company</p>
        <mat-form-field>
          <mat-select placeholder="Company" (selectionChange)="onCustomerChange($event)" value="company">
            <mat-option *ngFor="let company of alldata">
              {{ company.customer.customerName }}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>
    </mat-grid-tile>
    <mat-grid-tile *ngIf="companySites">
      <div>
        <p>Select site</p>
        <mat-form-field>
          <mat-select placeholder="Sites" (selectionChange)="onSitecChange($event)" value="site">
            <mat-option *ngFor="let site of alldata.sites; index as i">
              {{ site.buildingName }}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>
    </mat-grid-tile>
  </mat-grid-list>

and json:

"customer": {
"customerName": "ABC",
"customerAddress": "NYC",
"BillingAddress": "ABC",
"ProductInstallation": "drive A",
"sites": [
{
"_id": "5c5be1f2b73bc81dd404ac00",
"buildingName": "Main Building",
"requestDate": "12/02/2019",
"buildingAddress": "123 Street",
"numberofDrives": "3"
},
{
"_id": "5c5be1f2b73bc81dd404abff",
"buildingName": "second Building",
"requestDate": "12/02/2019",
"buildingAddress": "123 Street",
"numberofDrives": "2"
}
]
},
"_id": "5c5be1f2b73bc81dd404abfe",

now my json has hundreds of customers with multiple sites. I want to select company and then on second dropdown I want to show sites for selected sites

I have tried:

Object.values but that didn't work either

Muhammad Ali
  • 369
  • 7
  • 23
  • [See here how to render using condition](https://stackoverflow.com/questions/42931735/apply-a-condition-using-ngfor-and-ngif-in-angular-2) – Rowf Abd Feb 07 '19 at 08:47

2 Answers2

1

So what you can do is use the first dropdown to filter the list of objects and display the second dropdown only when a value has been selected by the user, this is done through *ngIf and the corresponding sites are shown for the same, please refer the below stackblitz.

<mat-grid-list cols="1">
    <mat-grid-tile>
        <div>
            <p>Select a company</p>
            <mat-form-field>
                <mat-select placeholder="Company" [(value)]="company">
                    <mat-option *ngFor="let company of alldata" [value]="company.customer">
                        {{ company.customer.customerName }}
                    </mat-option>
                </mat-select>
            </mat-form-field>
        </div>
    </mat-grid-tile>
    <mat-grid-tile *ngIf="company">
        <div>
            <p>Select site</p>
            <mat-form-field>
                <mat-select placeholder="Sites" [(value)]="site">
                    <mat-option *ngFor="let site of company.sites; index as i" [value]="site">
                        {{ site.buildingName }}
                    </mat-option>
                </mat-select>
            </mat-form-field>
        </div>
    <div>{{site | json}}</div>
    </mat-grid-tile>
</mat-grid-list>

StackBlitz

Naren Murali
  • 19,250
  • 3
  • 27
  • 54
0

just use *ngIf for that

      <mat-select placeholder="Sites" (selectionChange)="onSitecChange($event)" value="site">
        <ng-container *ngFor="let site of alldata.sites; index as i">
           <mat-option *ngIf="whatever site should be">
              {{ site.buildingName }}
           </mat-option>
        </ng-container>
      </mat-select>
    </mat-form-field>
Antoniossss
  • 31,590
  • 6
  • 57
  • 99