4
votes

I have a mat-select which consist of two options Individual Customers and Organizational Customers. If I select Ind Cust from drop down then I should see three option First Name,Last Name and Customer Id. But if I select Organization Customer I should be able to see Org Name and Org Id. I have all the HTML code ready however I am not able to display options based on selection.

HTML Code:

<mat-form-field>
<mat-label>Select an option</mat-label>
<mat-select [(value)]="selected">
  <mat-option value="indCust">IndividualCustomer</mat-option>
  <mat-option value="orgCust">Organizational Customer</mat-option>
</mat-select>
</mat-form-field>
<div class="row" *ngIf="indCust; orgCust">
// Code for Input Box of Individual Customer
</div> 
<ng-template #orgCust > 
//Code for dropdown of Organization Customer
</ng-template>

Typescript Code

  selected = '';
  indCust: Boolean ;
  if(this.selected == 'indCust'){
  this.indCust = true;
  } else {
  this.indCust = false;
  }

Can someone explain me what I am doing wrong and help me figure out correct way to do it.

2

2 Answers

4
votes

I would do like this:

HTML Code:

<mat-form-field>
    <mat-label>Select an option</mat-label>
    <mat-select>
        <mat-option *ngFor="let obj of list" (click)="get(obj)" [value]="obj"> {{obj.viewValue}}</mat-option>
    </mat-select>
</mat-form-field>

<span *ngIf="isSelected">
<div class="row" *ngIf="indCust">
    Code for Input Box of Individual Customer
   <mat-form-field class="example-full-width">
    <input matInput placeholder="Individual Customer" >
  </mat-form-field>
</div>
<div class="row" *ngIf="!indCust">
    Code for Combo Box of Organizational Customer
  <mat-form-field>
  <mat-label>Favorite food</mat-label>
  <mat-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
    </mat-option>
  </mat-select>
</mat-form-field>

</div>
</span>

TS Code:

import { Component } from '@angular/core';

@Component({
  selector: 'select-overview-example',
  templateUrl: 'select-overview-example.html',
  styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample {
  list: any[] = [
    { value: 'indCust', viewValue: 'IndividualCustomer' },
    { value: 'orgCust', viewValue: 'Organizational Customer' }
  ];


  foods: any[] = [
    { value: 'steak-0', viewValue: 'Steak' },
    { value: 'pizza-1', viewValue: 'Pizza' },
    { value: 'tacos-2', viewValue: 'Tacos' }
  ];

  isSelected: boolean = false;
  indCust: Boolean = undefined;

  get(data) {
    this.isSelected = true;
    if (data.value == 'indCust') {
      this.indCust = true;
      console.log(data)
    } else {
      this.indCust = false;
    }
  }
}

Stackblitz

1
votes

Ahmed try the following.

TS Component

sO = true;
boolOptions = [
    { Text: 'indCust', value: true },
    { Text: 'orgCust', value: false }
  ];

HTML

<mat-select [(ngModel)]="sO" name="sO" #sO="ngModel" placeholder="Option">
   <mat-option [value]="boolOption.value" *ngFor="let boolOption of boolOptions">{{boolOption.Text}}</mat-option>
</mat-select>

Update

to access the user selected value do this.sO from where you wish to use it.