Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 - How to show second dropdown data based on previous dropdown selection

Tags:

I am new to Ionic 2. I'm only have a surface basic of Ionic 2. I am trying to get a second dropdown based on the first selection. I already refer to the same problem but none of them using Ionic. Since I am new to Ionic 2, I dont know how to implement ionChange in Ionic 2. This is my part of code in .html

home.html

<ion-item>
  <ion-label>State</ion-label>
  <ion-select [(ngModel)]="state">
    <ion-option *ngFor = "let state of states">{{state.name}}   
    </ion-option>
  </ion-select>
</ion-item>

<ion-item>
  <ion-label>District</ion-label>
  <ion-select [(ngModel)]="district">
    <ion-option *ngFor = "let district of districts">{{district.name}}</ion-option>
  </ion-select>
</ion-item>

<ion-list>
  <ion-item *ngFor="let city of cities">
    <p>{{city.name}}</p>
  </ion-item>
</ion-list>

home.ts

Note: The actual data is many from the below data.

initializeState(){
  this.state = [
    {id: 1, name: 'Melaka'},
    {id: 2, name: 'Johor'},
    {id: 3, name: 'Selangor'}
  ];
}

initializeDistrict(){
  this.district = [
    {id: 1, name: 'Alor Gajah', state_id: 1, state_name: 'Melaka'},
    {id: 2, name: 'Jasin', state_id: 1, state_name: 'Melaka'},
    {id: 3, name: 'Muar', state_id: 2, state_name: 'Johor'},
    {id: 4, name: 'Segamat', state_id: 2, state_name: 'Johor'},
    {id: 5, name: 'Shah Alam', state_id: 3, state_name: 'Selangor'},
    {id: 7, name: 'Klang', state_id: 3, state_name: 'Selangor'}
  ];
}

initializeCity(){
  this.cities = [
    {id: 1, name: 'City of Alor Gajah 1', state_id: 1, district_id: 1},
    {id: 2, name: 'City of Alor Gajah 2', state_id: 1, district_id: 1},
    {id: 3, name: 'City of Jasin 1', state_id: 1, district_id: 2},
    {id: 4, name: 'City of Muar 1', state_id: 2, district_id: 3},
    {id: 5, name: 'City of Muar 2', state_id: 2, district_id: 3},
    {id: 6, name: 'City of Segamat 1', state_id: 2, district_id: 4},
    {id: 7, name: 'City of Shah Alam 1', state_id: 3, district_id: 5},
    {id: 8, name: 'City of Klang 1', state_id: 3, district_id: 6},
    {id: 9, name: 'City of Klang 2', state_id: 3, district_id: 6}
  ];
}

What I'm trying to do is first I want to get second dropdown based on the first dropdown. And then from both selection in first and second dropdown, I want to listing all the name of city.

like image 750
Akmal Avatar asked Dec 27 '16 03:12

Akmal


1 Answers

Create a change event handler on the first select element. Then have that call a method, passing the selected value with it. Then do some checks on the selected values to determine if you want to show the next select and then the list.

*.html ------- template
<ion-content padding>
     <ion-item>
         <ion-label>State</ion-label>
         <ion-select (ionChange)="setDistrictValues(sState)"[(ngModel)]="sState">
             <ion-option [value]="sState" *ngFor="let sState of states">{{sState.name}} </ion-option>
         </ion-select>
     </ion-item>
     <ion-item *ngIf="selectedDistricts">
        <ion-label>District</ion-label>
        <ion-select (ionChange)="setCityValues(sDistrict)" [(ngModel)]="sDistrict">
            <ion-option [value]="sDistrict" *ngFor="let sDistrict of selectedDistricts">{{sDistrict.name}}</ion-option>
        </ion-select>
     </ion-item>
     <ion-list *ngIf="selectedCities">
         <ion-item *ngFor="let city of selectedCities">
              <p>{{city.name}}</p>
         </ion-item>
     </ion-list>

*.ts ------- controller/component
    public states: any[];
    public districts: any[];
    public cities: any[];

    public selectedDistricts: any[];
    public selectedCities: any[];

    public sState: any;
    public sDistrict: any;

    appName = 'Ionic App';

    constructor(public navController: NavController) { 
        this.initializeState();
        this.initializeDistrict();
        this.initializeCity();
    }

    initializeState(){
    this.states = [
        {id: 1, name: 'Melaka'},
        {id: 2, name: 'Johor'},
        {id: 3, name: 'Selangor'}
    ];
    }

    initializeDistrict(){
    this.districts = [
        {id: 1, name: 'Alor Gajah', state_id: 1, state_name: 'Melaka'},
        {id: 2, name: 'Jasin', state_id: 1, state_name: 'Melaka'},
        {id: 3, name: 'Muar', state_id: 2, state_name: 'Johor'},
        {id: 4, name: 'Segamat', state_id: 2, state_name: 'Johor'},
        {id: 5, name: 'Shah Alam', state_id: 3, state_name: 'Selangor'},
        {id: 7, name: 'Klang', state_id: 3, state_name: 'Selangor'}
    ];
    }

    initializeCity(){
    this.cities = [
        {id: 1, name: 'City of Alor Gajah 1', state_id: 1, district_id: 1},
        {id: 2, name: 'City of Alor Gajah 2', state_id: 1, district_id: 1},
        {id: 3, name: 'City of Jasin 1', state_id: 1, district_id: 2},
        {id: 4, name: 'City of Muar 1', state_id: 2, district_id: 3},
        {id: 5, name: 'City of Muar 2', state_id: 2, district_id: 3},
        {id: 6, name: 'City of Segamat 1', state_id: 2, district_id: 4},
        {id: 7, name: 'City of Shah Alam 1', state_id: 3, district_id: 5},
        {id: 8, name: 'City of Klang 1', state_id: 3, district_id: 6},
        {id: 9, name: 'City of Klang 2', state_id: 3, district_id: 6}
    ];
    }

    setDistrictValues(sState) {
        this.selectedDistricts = this.districts.filter(district => district.state_id == sState.id)
    }

    setCityValues(sDistrict) {
        this.selectedCities = this.cities.filter(city => city.district_id == sDistrict.id);
    }

Select your state, filter for only the districts that have a state_id that matches the selected state ID. The same thing is done for the cities.

Working version: https://embed.plnkr.co/Qu1lL6PDIX2DTDGv01ZI/

like image 156
Pezetter Avatar answered Sep 26 '22 16:09

Pezetter