gpt4 book ai didi

typescript - Ionic 2 - 如何根据先前的下拉选择显示第二个下拉数据

转载 作者:搜寻专家 更新时间:2023-10-30 20:57:54 26 4
gpt4 key购买 nike

我是 Ionic 2 的新手。我只有 Ionic 2 的表面基础。我正在尝试根据第一个选择获得第二个下拉列表。我已经提到了同样的问题,但他们都没有使用 Ionic。由于我是 Ionic 2 的新手,我不知道如何在 Ionic 2 中实现 ionChange。这是我在 .html 中的部分代码

主页.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>

首页.ts

注意:实际数据多出自以下数据。

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

我想做的是首先我想根据第一个下拉菜单获得第二个下拉菜单。然后从第一个和第二个下拉列表中的选择中,我想列出所有城市的名称。

最佳答案

在第一个选择元素上创建一个更改事件处理程序。然后调用一个方法,将选定的值传递给它。然后对所选值进行一些检查以确定是否要显示下一个选择,然后显示列表。

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

选择您的州,仅过滤具有与所选州 ID 相匹配的 state_id 的地区。同样的事情也适用于城市。

工作版本: https://embed.plnkr.co/Qu1lL6PDIX2DTDGv01ZI/

关于typescript - Ionic 2 - 如何根据先前的下拉选择显示第二个下拉数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com