Pradnya Bhagat
Pradnya Bhagat

Reputation: 219

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);
}}

Upvotes: 2

Views: 1006

Answers (2)

Preeti
Preeti

Reputation: 71

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>

Upvotes: 4

Setu Kumar Basak
Setu Kumar Basak

Reputation: 12022

In your .html file

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

  <ion-item  *ngIf="showPaymentLocation"> // decide whether to show or hide
    <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>

// in your .ts file

showPaymentLocation: boolean = false;

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

paymentModeChosen(paymentmodeName): void {
  if(paymentmodeName == 'Bank') {
    this.showPaymentLocation = false;
  } else { 
    this.showPaymentLocation = true;
   }
}

Upvotes: 2

Related Questions