简体   繁体   English

如何设置多个选项的默认选择值(离子选择)

[英]How to set default selected value of multiple options(ion-select)

I am trying to have certain pre-selected values in my ion-select checkbox, but I was unable to find any answers. 我试图在“ ion-select复选框中具有某些预先选择的值,但是我找不到任何答案。 The values I want pre-selected have the value in my object as checked=true . 我要预先选择的值在我的对象中具有的值是checked=true

 private totalPPE:any = [ {id: 0,text:"Gloves",checked:true}, {id: 1,text:"Glasses/Goggles/Face-Shield",checked:true}, {id: 2,text:"Hard Hat",checked:true}, {id: 3,text:"Hearing Protection",checked:false}, {id: 4,text:"FR Attire",checked:true}, {id: 5,text:"Steel Toe Boots",checked:true}, {id: 6,text:"Fall Protection",checked:false}, {id: 7,text:"H2S Monitor",checked:false}, {id: 8,text:"Respiratory Protection",checked:false}, {id: 9,text:"Other",checked:false}]; 
 <ion-item> <ion-label>PPE REQUIRED FOR TASK</ion-label> <ion-select [(ngModel)]="totalPPE" name="ppe" multiple="true"> <ion-option *ngFor="let ppe of totalPPE; let i = index" [selected]="ppe.checked=='true'" [value]=ppe.text>{{ppe.text}}</ion-option> </ion-select> </ion-item> 

All of the resources I tried following unfortunately didn't work for me. 不幸的是,我尝试使用的所有资源都不适合我。 I even tried the question, How to set default selected value of ion-option? 我什至尝试了一个问题, 如何设置离子选项的默认选定值? , but unfortunately I found no solutions either. ,但很遗憾,我也没有找到解决方案。

So the issue was the object array. 因此,问题在于对象数组。 I don't believe Ionic can handle them so I changed the array to this: 我不相信Ionic可以处理它们,所以我将数组更改为:

 private totalPPE:any = ["Gloves", "Glasses/Goggles/Face-Shield", "Hard Hat", "Hearing Protection", "FR Attire", "Steel Toe Boots", "Fall Protection", "H2S Monitor", "Respiratory Protection", "Other"]; 

And added another array for default values: 并添加了另一个数组作为默认值:

 private defaultPPE:any = ["Gloves", "Glasses/Goggles/Face-Shield", "Hard Hat", "FR Attire", "Steel Toe Boots"]; 

And changed my HTML: 并更改了我的HTML:

 <ion-item> <ion-label class="ppe">PPE REQUIRED FOR TASK</ion-label> <ion-select [(ngModel)]="defaultPPE" name="ppe" multiple="true"> <ion-option *ngFor="let ppe of totalPPE" value={{ppe}} >{{ppe}}</ion-option> </ion-select> </ion-item> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM