Search code examples
htmltypescriptionic3dropdownshow-hide

Show/Hide Second DropDownSelect Based on First DropDownSelect in Ionic 3


I'm very new in ionic, I'm trying to hide second dropdown based on selection of first dropdown. My first DropDown 1.Bank 2.Cash When i select Bank Second DropDown will be hidden and when select Cash then it will show second DropDown.

.html file

<ion-list no-lines>
  <ion-item>
    <ion-label>Payment Mode</ion-label>
    <ion-select [(ngModel)]="paymentmode" (ionChange)="paymentModeChosen()">
      <ion-option [value]="paymentmode" *ngFor="let paymentmode of 
         paymentmodes()">{{paymentmode}}</ion-option>
     </ion-select>
   </ion-item>

  <ion-item  *ngIf="paymentlocations">
    <ion-label>Payment Location</ion-label>
    <ion-select [(ngModel)]="paymentlocation" 
       (ionChange)="paymentLocationChosen()">
    <ion-option  [value]="paymentlocation" *ngFor="let paymentlocation of 
      paymentlocations()">{{paymentlocation}}</ion-option>
  </ion-select>
</ion-item>
</ion-list>

.ts file

paymentmodes(): string[] {
return [
  "Bank",
  "Cash"
]; }
 paymentmode: string = "Bank";

 paymentlocations(): string[] {
return [
  "Ahmadabad",
  "Chennai",
  "Delhi",
  "Kolkata",
  "Mahad",
  "Mumbai",
  "Pune",
  "Roha",
  "Sahibabad"
];
}

paymentlocation: string = "Ahmadabad";

constructor(public navCtrl: NavController, public navParams: NavParams) {}

paymentModeChosen(): void {
  console.log(this.paymentmode);}

paymentLocationChosen():void{
 console.log(this.paymentlocation);
}}

Solution

  • changes in .ts file

    create new variable in .ts file

    public paymentL = true;
    

    then modify this function

    paymentModeChosen(): void {
        console.log(this.paymentmode);
        if(this.paymentmode == 'Cash'){
          this.paymentL = false;
        }else{
           this.paymentL = true;
        }
    
      }
    

    Changes in html file

    In html file ngif "paymentL" if paymentL is "Cash" then this ion-item not visible and if "Bank" then ion-item will be visible.

    <ion-item  *ngIf="paymentL">
        <ion-label>Payment Location</ion-label>
        <ion-select [(ngModel)]="paymentlocation" 
           (ionChange)="paymentLocationChosen()">
        <ion-option  [value]="paymentlocation" *ngFor="let paymentlocation of 
          paymentlocations()">{{paymentlocation}}</ion-option>
      </ion-select>
    </ion-item>