Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default selected value of ion-option?

I'm using Ionic v2 and I can not set the selected value when showing the page.

<ion-select [(ngModel)]="company.form">     <ion-option *ngFor="let form of forms" [value]="form" [selected]="true">{{form.name}}</ion-option> </ion-select> 

I've tried with checked, too but that isn't work either. How can I do this?

Cordova CLI: 6.4.0 Ionic Framework Version: 2.0.0-rc.3 Ionic CLI Version: 2.1.13 Ionic App Lib Version: 2.1.7 Ionic App Scripts Version: 0.0.45 
like image 386
Perrier Avatar asked Dec 14 '16 15:12

Perrier


People also ask

How do I set default value in ion-select option?

If you deal with default value xor objects, the cleanest way is to provide a compare function to the [compareWith] attribute. This function takes 2 objects in parameters and returns true if they are equals. This way, existing values in model will be selected at start.

How do you get selected value from ion-select?

We can access the Ionic select by using a standard <ion-select> element. A select component always used with child <ion-select-option> element. If the <ion-select-option> does not have a value attribute, then its text will be used as the value.

How do I reset my ion-select?

You cannot use ion-option to reset ion-select form field. But you can provide clear button to reset ion-select if an ion-option is selected as below.


2 Answers

If you deal with default value xor objects, the cleanest way is to provide a compare function to the [compareWith] attribute. This function takes 2 objects in parameters and returns true if they are equals. This way, existing values in model will be selected at start. This works too if new data are added in model outside of the select.

Following example is taken from official Ionic doc

<ion-item>   <ion-label>Employee</ion-label>   <ion-select [(ngModel)]="employee" [compareWith]="compareFn">     <ion-option *ngFor="let employee of employees" [value]="employee"></ion-option>   </ion-select> </ion-item>  compareFn(e1: Employee, e2: Employee): boolean {   return e1 && e2 ? e1.id == e2.id : e1 == e2; } 

EDIT : using double equals make it work for Ionic 4 (as Stan Kurdziel suggests in comment)

like image 76
Nicolas Janel Avatar answered Sep 22 '22 18:09

Nicolas Janel


The problem seems to be that ion-option don't like objects in rc3. I have to work with only the id part of the object and write a seperate changehandler that find the needed object and set it as a value.

  <ion-select [ngModel]="company.form.id" (ngModelChange)="companyFormSelected($event)" okText="Ok" cancelText="Mégsem">     <ion-option *ngFor="let form of forms" [value]="form.id" [selected]="form.id == company.form.id">{{form.name}}</ion-option>   </ion-select> 

And the changehandler:

companyFormSelected(newform) {     let selectedForm = this.forms.find((f)=>{       return f.id === newform;     });     this.company.form=selectedForm; } 

This seems to be a bug in rc3 but I don't know where can I report bugs. I did open a topic on ionic forum.

like image 35
Perrier Avatar answered Sep 21 '22 18:09

Perrier