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

时间:2018-11-27 21:03:07

标签: angular ionic-framework ionic3 ion-select

我正在尝试在我的ion-select复选框中选择某些预先选择的值,但是我找不到任何答案。我要预选的值在我的对象中的值为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>

不幸的是,我尝试使用的所有资源对我来说都不起作用。我什至尝试了How to set default selected value of ion-option? 这个问题,但不幸的是,我也没有找到解决方案。

1 个答案:

答案 0 :(得分:0)

所以问题出在对象数组上。我不相信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"];

并为默认值添加了另一个数组:

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

并更改了我的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>