简体   繁体   中英

How to enable/disable an Ionic >=2 alert button if there are no input selections

I have an Ionic alert with radio button options, and would like to disable an OK button until the user has made a selection. Very similar to this post , except I want to do it on an action from the alert itself and be able to update the button "enabledness", so my setup would be like the following

  import { AlertController } from 'ionic-angular';

    export class MessageService {
      constructor(private alertCtrl: AlertController) {
      }

      presentAlert() {
        let alert = this.alertCtrl.create();

        alert.addInput({
          type: 'radio',
          label: 'Option 1',
          value: 'value1',
          checked : false
        });

        alert.addInput({
          type: 'radio',
          label: 'Option 2',
          value: 'value2',
          checked : false
        });

        alert.addButton('Cancel');
        alert.addButton({
          text: 'Ok',

          handler: data => {

          }
        });
        alert.present();
      }
    }

I would like the ok button to be initially disabled, but then enabled as soon as the user makes a selection. Since the ion-select also uses the alert, it would be good to be able to use the same thing there.

Is this possible / is there an easy way to do this?

Thanks in advance for any help.

I might be late but this worked for me.

var bool=true;

and then in addInput

disable:bool

I am not sure why I had to do it this way, but it works.

try this.

presentPrompt() {
    let alert = this.alertCtrl.create({
      title: 'Cancel Prompt',
      inputs: [
        {
          type:'radio',
          label:'Cancel for reason 1',
          value:'Cancel for reason 1'
        },
        {
            type:'radio',
            label:'Cancel for reason 2',
            value:'Cancel for reason 2'
        }
      ],
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: data => {}
        },
        {
          text: 'OK',
          handler: data => {            
            if (data) {
              console.log(data          
            } else {
              this.presentAlert();
            }
          }
        }
      ]
    });
    alert.present();
  }

  private presentAlert() {
    let alert = this.alertCtrl.create({
      title: 'Validation select',
      subTitle: 'Please select',
      buttons: [{
                text:'Ok',
                handler:data=>{
                  this.presentPrompt();
                }
              }]
    });
    alert.present();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM