ionic3 多級聯動城市選擇插件 ion-multi-picker


1、效果演示

2、npm安裝擴展包依賴  ion-multi-picker 組件

npm install ion-multi-picker --save

3、在app.module.ts中導入插件模塊

 1 import { MultiPickerModule } from 'ion-multi-picker';
 2 @NgModule({
 3   declarations: [
 4     .....
 5   ],
 6   imports: [
 7     IonicModule.forRoot(MyApp),
 8     //Import MultiPickerModule 模塊
 9     MultiPickerModule 
10   ],
11   bootstrap: [IonicApp],
12   entryComponents: [
13     .....
14   ],
15   providers: []
16 })
17 export class AppModule {}    

4、具體實例使用如下:

(1)獲取本地省市區json數據

  

 1 //定義省市區數據源變量
 2  public cityList = {
 3    area:[]
 4 }; 
 5 //userInfo即為最終選中的省市區數據
 6 public userInfo = {
 7      province:"",
 8     city:"",
 9     district:""
10 };
11 // 獲取本地城市列表服務
12 getCityData() {
13     return Observable.create(observer => {
14         this.http.get("./assets/area-data.json").subscribe(res => {
15             this.cityList['area'] = res;
16         }, err => {
17              this.handleError(err);
18         })
19     });
20 }
21 //獲取選定的省市區
22 public getCityArea(){
23     let cityArr = document.getElementById("cities").innerText;
24     cityArr = this.Validate.trimBlank(cityArr);
25     if(cityArr == '省-市-區(縣)'){
26      this.Pop.toast("請選擇所在地區");
27      return false;
28     }
29     this.cityArr = cityArr.split("-");
30     this.userInfo.province  = this.cityArr[0];
31     this.userInfo.city  = this.cityArr[1];
32     this.userInfo.district  = this.cityArr[2];
33     return this.userInfo;
34 }

5、模板渲染

1 <ion-item>
2     <ion-label>城市</ion-label>
3     <ion-multi-picker id="cities" item-content [cancelText]="'取消'" [doneText]="'完成'" [placeholder]="userInfo.province == undefined ? '省-市-區(縣)' : (userInfo.province + '-'+ userInfo.city +'-'+ userInfo.district)" [separator]="'-'" [multiPickerColumns]="cityList['area']"></ion-multi-picker>
4 </ion-item>

注意:其中 

   [multiPickerColumns]屬性用於渲染獲取到的本地省市區數據源

  [placeholder]屬性用於初始化(已選中或未選中的)當前地區
  [cancelText]屬性為取消按鈕
  [cancelText]屬性為完成按鈕
  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM