Ionic2 Ion-select without OK and Cancel

前端 未结 3 914
梦谈多话
梦谈多话 2020-12-17 20:57

I have built an Ionic2 app and I am currently trying to improve the UX. To do that, I received some feedback that made me think of whether there is a way of having a

相关标签:
3条回答
  • 2020-12-17 21:18

    Changing the API used in the select element (by using the ActionSheet API) could be an option.

    In order to do that, 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.


    EDIT:

    Just like you can find in Ionic 2 docs

    If the number of options exceed 6, it will use the alert interface even if action-sheet is passed.

    0 讨论(0)
  • 2020-12-17 21:22

    I know this thread is 7 months old and the OP is probably long past this question but since the feature is yet to be added to ionic framework, I'm adding the workaround I came up with for other people's reference.

    CSS PART

    Intuitively the first thing you need to do is to add some sass/css to hide the unwanted buttons. I did that by passing a css class "remove-ok" to selectOptions for my ion-select element. (I'm only removing OK button but if someone needs to remove cancel button as well, that's an easy css modification).

    In component:

    this.selectOptions = {
          cssClass: 'remove-ok'
        };
    

    and in HTML:

    <ion-select [selectOptions]="selectOptions">
    

    And in app.scss add:

    .remove-ok {
        .alert-button:nth-child(2) {
            display: none;
        }
    }
    

    SCRIPT PART

    Now that the OK button is hidden, you will need to somehow close the alert view.

    To invoke click() event on the hidden OK button is straightforward and intuitive but you'll soon find out that although it works perfectly on ionic serve, it won't work on an actual iOS device.

    The solution is to find reference to the alert view, pass the checked option to the select handler, and finally dismiss the view.

    So in ngOnInit in your component class, you'll need this:

    ngOnInit() {
            let root = this.viewController.instance.navCtrl._app._appRoot;
            document.addEventListener('click', function(event){
            let btn = <HTMLLIElement> document.querySelector('.remove-ok .alert-button-default:nth-child(2)');
            let target = <HTMLElement> event.target;
            if(btn && target.className == 'alert-radio-label')
            {
                  let view = root._overlayPortal._views[0];
                  let inputs = view.instance.d.inputs;
                  for(let input of inputs) {
                    if(input.checked) {
                      view.instance.d.buttons[1].handler([input.value]);
                      view.dismiss();
                      break;
                    }
                  }
                }
            });
        }
    

    Again, if you intend to remove the Cancel button as well, mind the css references in this code.

    0 讨论(0)
  • 2020-12-17 21:42

    Pass empty value :

    <ion-select okText="" cancelText="">
          <ion-option>
          </ion-option>
        </ion-select>
    

    It is working in my case.

    0 讨论(0)
提交回复
热议问题