Search code examples
angularionic-frameworkionic4ion-select

How to get value of a ion-select ? My value only appear when I click on the field


I develop a mobile application with ionic and I have a problem with the ion-select tag. The ion-select value does not seems to load UNTIL I click on it.

<ion-item>
    <ion-label position="stacked" style="color: darkgrey">Etat intervention {{liste[0].id_etat_intervention}} - {{etat_select}}</ion-label>
    <ion-select okText="OK" cancelText="Annuler" [value]="liste[0].id_etat_intervention">
        <ion-select-option *ngFor="let etat of etats_intervention" [value]="etat.id" >{{etat.id}} {{etat.libelle}}</ion-select-option>
    </ion-select>
</ion-item>

On my ts file :

await this.dbProvider.getEtatsIntervention().then((data) => {
    this.etats_intervention = data;
});

On my provider file :

getEtatsIntervention () {
        const req = 'SELECT id, libelle FROM etat_intervention ORDER BY libelle ASC ;';

        return this.database.executeSql(req, []).then (result => {
            const infos = [];
            if (result.rows.length > 0) {
                for (let i = 0; i < result.rows.length; i++) {
                    infos.push({
                        id: result.rows.item(i).id,
                        libelle: result.rows.item(i).libelle});
                }
            }
            return infos;
        }).catch(e => console.log('erreur database.getEtatsIntervention() ' + e.error));
    }

And the value 'liste[0].id_etat_intervention' is recovered in the same way when the form is loaded

Here what's happend :

When the form is load

When I click on the field

I tried everything I'm stuck ... Thanks for your help !


Solution

  • I found a solution :

    <ion-item>
        <ion-label position="stacked" style="color: darkgrey">Etat intervention</ion-label>
        <ion-select okText="OK" cancelText="Annuler" [selectedText]="etat_select" (ionChange)="onChangeEtat($event)">
            <ion-select-option *ngFor="let etat of etats_intervention" [value]="etat.libelle">{{etat.libelle}}</ion-select-option><!-- [value]="etat.id"  -->
        </ion-select>
    </ion-item>
    
    
    onChangeEtat(value) {
        this.etat_select = value.target.value;
    }
    
    getEtat() {
        for (const i in this.etats_intervention) {
            if (this.etats_intervention[i].id === this.liste[0].id_etat_intervention) {
                this.etat_select = this.etats_intervention[i].libelle;
            }
        }
    }