4

I'm working on ionic2 project. I use ion-select element.

I search after a way to close the select-box programmatically on select any item (not wait the user to press OK).

  <ion-select id="select" #select>
      <ion-option (ionSelect)="closeAndSave()" *ngFor="let option of enumList" [value]="option">{{ option}}</ion-option>
    </ion-select>

`

class myClass{
    @viewChild('select') select:Select;
    closeAndSave(){
        /*it is come here on press any option. but how can I close here my select element? I tried: this.select.destroy()  - not do any thing. any solution?*/
    }

`

Cœur
  • 37,241
  • 25
  • 195
  • 267
user5260143
  • 1,048
  • 2
  • 12
  • 36

4 Answers4

7

this is what I did:

this.court = val;
this.select.close();

put this inside your closeAndSave function.

Before that u need to pass some value from your closeAndSave function like:

(ionSelect)="closeAndSave('someValueHere')"

then use this value there in you TS code:

import { Select } from 'ionic-angular';

class myClass{
    @ViewChild(select) select:Select;
    closeAndSave(val){
    this.someVar = val;
        this.select.close();
    }
}
ascripter
  • 5,665
  • 12
  • 45
  • 68
yash_DedSec
  • 251
  • 2
  • 11
1

try to test this tag:

you only need to add interface="action-sheet" in the ion-select element.

  <ion-item>
    <ion-label>Gender</ion-label>
    <ion-select interface="action-sheet">
      <ion-option value="f" selected="true">Female</ion-option>
      <ion-option value="m">Male</ion-option>
    </ion-select>
  </ion-item>

I'm not sure if that's a valid option (in terms of UX) in your app.

source : https://stackoverflow.com/a/39015858/308578

saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
1

I used this: (document.querySelector('ion-backdrop') as HTMLElement).click();

-1

I'm not sure if this works, but it is shown as a popup and popups can be dismissed with the ionic ViewController.

You can use it like this:

import { ViewController } from 'ionic-angular'
...
export class YourComponent {
    constructor(private viewCtrl: ViewController){}
    closeAndSave(){
        //save things here 
        this.viewCtrl.dismiss();
    }
}
KaFu
  • 2,284
  • 1
  • 11
  • 12